Anonymní profil peter – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil peter – Programujte.comAnonymní profil peter – Programujte.com

 

Příspěvky odeslané z IP adresy 2001:718:2601:26c:79ca:ee...–

peter
JavaScript, AJAX, jQuery › tip: js/webrtc chat pres stu…
9. 2. 2016   #208509

Kdyby jste se nekdo moc nudil, mam tu tip pro js chat, ktery vyuziva stun server, treba googlu.

Dole kod pro html chat pro firefox pres stun server. Ted je tam nastaveny prazdny server=null, takze je treba to spustit pro 2 lidi ve dvou oknech firefoxu. Jeden vytvari mistnost (hosting), ostatni se pripojuji (guest), jestli to chapu spravne.

Okno1 - vytvorit channel (vygeneruje kod)
Okno1 - zkopirovat JSON kod z textarea u join do schranky
Okno2 - vlozit JSON kod ze schranky a kliknut na join (vygeneruje kod)
Okno2 - zkopirovat kod u ansfer
Okno1 - vlozit kod ansfer a kliknut na ansfer
A ted je mozne si psat.

Dalo mi dost prace najit funkcni kod, ktery generuje vubec nejake session-description.
Googlem spis najdete videochat, nahravani z web-cam a pod, pomoci webRTC.

<html>
<!--
http://pastebin.com/g2YVvrRd
http://stackoverflow.com/…-datachannel
-->
    <head>
        <title>WebRTC Basic</title>
    </head>
    <body>
        <header><h1>WebRTC Basic</h1></header>

        <section>

            <div id="info" class="disconnected">
                you must first create or join a channel
            </div>

            <form id="signal" action="">
                <p>
					<label for="channel">channel: </label>
					<input id="channel" type="text" value="test" />
					<button id="create">create</button>
                </p>

                <p>
					<label for="offer">offer: </label>
					<textarea id="offer" rows="8" cols="60"></textarea>
					<button id="join">join</button>
                </p>

                <p>
					<label for="answer">answer: </label>
					<textarea id="answer" rows="8" cols="60"></textarea>
					<button id="submitAnswer" disabled>submit answer</button>
                </p>
            </form>

            <form id="chat" action="" onsubmit="return false;">
                <label for="message"> message: </label>
                <input id="message" autocomplete="off" disabled />
                <input type="button" id="send" disabled value="send">
            </form>

            <div id="messages"></div>

        </section>
        <!--script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script-->
<script type="text/javascript">

var host = null, guest = null;

//var PeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
//var IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
//var SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;

var channelID;

var RTC = {};
RTC.conn      = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;;
RTC.session   = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription || window.msRTCSessionDescription;
RTC.candidate = window.RTCIceCandidate || window.mozRTCIceCandidate;

var cfg = {}
cfg.server = {};
//cfg.server.url = {url: ['stun:stun.l.google.com 19302']};
//cfg.server.url = {iceServers: [{url:'stun:stun.l.google.com 19302'}]};
cfg.server.url = null;
cfg.server.opt = null;
cfg.server.opt = {optional: [{'DtlsSrtpKeyAgreement': true}, {'RtpDataChannels': true}]};

function setChannelEvents(channel) {
	channel.onmessage = function (event) {
		var message = event.data || event;
		console.log('** channel.onmessage: ', message);
	$("messages").innerHTML += '<br>'+message;
	};

	channel.onopen = function () {
		console.log('** channel.onopen');
		// and finally we can do this
		channel.send('hello world!');
	$("messages").innerHTML += '<br>sending';

	};

	channel.onerror = function (err) {
		console.error('** channel.onerror: ', err);
	};

	channel.onclose = function (err) {
		console.log('** channel.onclose: ', err);
	};
}


// ---------------------------------------------------------------------
// Host object creates the offer for a WebRTC session
function classHost(id) {
	var self = this;
	this.id = id;
	this.peer = new RTC.conn(cfg.server.url,cfg.server.opt);
	this.dataChannel = this.peer.createDataChannel(id, {
		reliable: true
	});

	this.peer.onicecandidate = function (event) {
		if (!event.candidate) { // when ice is complete, there is an event without a candidate (on Chrome anyhow)
			console.log(self.peer);
			self.offer = self.peer.localDescription;
			self.cb(self.offer);
		}
	}

	setChannelEvents(this.dataChannel);
}

classHost.prototype.createOffer = function (cb) {
	var self = this;

	this.peer.createOffer(function (offer) {
		console.log('Host.peer.createOffer callback: ', offer);
		self.cb = cb;
		self.peer.setLocalDescription(offer, function () {
			// We wait for ICE to complete before continuing
			//self.offer = offer;
			//cb(offer);
		}, function (err) {
			console.error(err);
		});
	}, function (err) {
		console.error(err);
	}, null);

}

classHost.prototype.setRemoteDescription = function (answer, cb) {
	var self = this;
	console.log('Host.setRemoteDescription: ', answer);

	var _sd = new RTC.session(answer);

	this.peer.setRemoteDescription(_sd, cb, function (err) {
		console.error(err);
	});
};


// ---------------------------------------------------------------------
// Guest object answers the offer from the host
function classGuest(id) {
	this.id = id;
	this.peer = new RTC.conn(cfg.server.url,cfg.server.opt);

	this.dataChannel = this.peer.createDataChannel(id, {
		reliable: true
	});

	var self = this;
	this.peer.ondatachannel = function (event) {
		console.log('Guest.peer.datachannel: ', event);
		self.dataChannel = event.channel;
		setChannelEvents(self.dataChannel);
	};
	this.peer.onicecandidate = function (event) {
		if (!event.candidate) { // when ice is complete, there is an event without a candidate (on Chrome anyhow)
			self.answer = self.peer.localDescription;
			self.cb(self.answer);
		}
	}
}

classGuest.prototype.setRemoteDescription = function (offer, cb) {
	var self = this;
	var _desc = new RTC.session(offer);
	console.log('Guest desc: ', _desc, offer);

	this.peer.setRemoteDescription(_desc, function () {
		self.offer = offer;
		cb();
	}, function (err) {
		console.error(err);
	});
};

classGuest.prototype.createAnswer = function (cb) {
	var self = this;

	this.peer.createAnswer(function (answer) {
		console.log('Guest.peer.createAnswer callback: ', answer);
		self.answer = answer;
		self.cb = cb;
		self.peer.setLocalDescription(answer, function () {
			// Again, waiting for ICE
			//cb(answer);
		}, function (err) {
			console.error(err);
		});
	}, function (err) {
		console.error(err);
	});
};


// ---------------------------------------------------------------------
//alert(JSON);
function $(id)
{return document.getElementById(id);}

//			$('document').ready(function () {

$("create").onclick = function () {
	$("join").disabled = true;
	$("submitAnswer").disabled = false;

	channelID = $("channel").value;
	host = new classHost(channelID);
	host.createOffer(function (offer) {
		$('offer').value = JSON.stringify(offer);
	})
	return false;
};


$("join").onclick = function () {
	//$("#create").attr("disabled", "disabled");
	//$("#submitAnswer").attr("disabled", "disabled");
	$("create").disabled = true;
	$("submitAnswer").disabled = false;

	channelID = $("channel").value;
	offer = $("offer").value;
	guest = new classGuest(channelID);
	guest.setRemoteDescription(JSON.parse(offer), function () {
		console.log('Guest.setRemoteDescription completed');
		guest.createAnswer(function (answer) {
			$("answer").value = JSON.stringify(answer);
		});
//	guest.dataChannel.send('Guest joined');
	$("message").disabled = false;
	$("send").disabled = false;
	});
	return false;
};

$("submitAnswer").onclick = function () {
	var answer = $("answer").value;
	host.setRemoteDescription(JSON.parse(answer), function () {
		console.log('-- complete');
		// We wait before the data channel is set up, before we transmit anything (peer.onremotedatachannel)
		//host.dataChannel.send('hello world!');
	});
//	host.dataChannel.send('Host joined');
	$("message").disabled = false;
	$("send").disabled = false;
	return false;
};

$("send").onclick = function () {
	var message = $("message").value;
	var name = host && host.id ? 'host' : (guest && guest.id ? 'guest' : 'unknown')
	$("messages").innerHTML += '<br>'+name+'> '+message;
	if (host && host.id)  {host.dataChannel.send(name+'> ' + message);}
	if (guest && guest.id) {guest.dataChannel.send(name+'> ' + message);}
	return false;
};
//			});

/*
$(document).ready(function () {

	$("#create").click(function () {
		$("#join").attr("disabled", "disabled");
		$("#submitAnswer").removeAttr("disabled");

		channelID = $("#channel").val();
		host = new Host(channelID);
		host.createOffer(function (offer) {
			$('#offer').val(JSON.stringify(offer));
		})
		return false;
	});


	$("#join").click(function () {
		$("#create").attr("disabled", "disabled");
		$("#submitAnswer").attr("disabled", "disabled");

		channelID = $("#channel").val();
		offer = $("#offer").val();
		guest = new Guest(channelID);
		guest.setRemoteDescription(JSON.parse(offer), function () {
			console.log('Guest.setRemoteDescription completed');
			guest.createAnswer(function (answer) {
				$("#answer").val(JSON.stringify(answer));
			});
		});
		return false;
	});

	$("#submitAnswer").click(function () {
		var answer = $("#answer").val();
		host.setRemoteDescription(JSON.parse(answer), function () {
			console.log('-- complete');
			// We wait before the data channel is set up, before we transmit anything (peer.onremotedatachannel)
			//host.dataChannel.send('hello world!');
		});
		return false;
	});

});
*/
        </script>
    </body>
</html>
peter
PHP › Interní prolinkování (nahraz…
9. 2. 2016   #208506

jeste bych to zmenil
'~(\<(\w+)(\W.*)*?\>).*?\<\/\2?\>)|(\<(\w+)(\W.*)*?\>)~i',

\w+.* je totez pro deleni abcd na abcd (spolu nebo) ab cd (jakkoliv zvlast), takze tam musi byt za textem netextovy znak a ten tam muze ci nemusi byt

(\w+)(\W.*)*
\w+ = pismena jmena tagu pripadne tam muzes dat [\w\d]+ necisla, nepismena
\W = nepismenovy znak
.* = jakykoliv znak v poctu 0-nekonecno
(\W.*)* = vyskyt retezce, ktery zacina nepismenkovym znakem (popsano vys, dva radky) a opakuje se 0-nekonecno
\> = znak ukonceni tagu ">"
?\> = hledej, dokud nenarazis na retezec ">" (ukonceni tagu), kdybys tam otaznik nedal, tak .* pred tim je jakykoliv  znak a tam patri i ukonceni tagu ">"

Ps. Vsechny ty vyrazy mimo php kod jsou netestovane. Pouze tipuji, jak by to mohlo vypadat. Doladis sam.

peter
PHP › Interní prolinkování (nahraz…
9. 2. 2016   #208505

A taky ten vyraz muze byt jiny.
'~(\<(a)\W.*?\>).*?\<\/\2?\>)|(\<(img) .*?\>)~i', // a+ne_pismeno+cokoliv
'~(\<(\w+).*?\>).*?\<\/\2?\>)|(\<(\w+).*?\>)~i', // pismeno_opakovani+cokoliv - to by melo resit jakekoliv tagy, nejdriv se pokusi najit pary. Jenze, opet to selze na zanorenem tagu, treba <b>text<i>text</i>text</b>, cele to odstrani do $list
 

peter
PHP › Interní prolinkování (nahraz…
9. 2. 2016   #208504

V tom druhem callback je samozrejme nesmysl radek
$list[] = $matches[0];
Jako, on neskodi, viditelne, ale... Chyba kopirovani :)
 

peter
PHP › Interní prolinkování (nahraz…
9. 2. 2016   #208503

To uplne nesnasim, kdyz jsou v clanku zamichane odkazy a do toho jeste fake-odkazy na jine clanky jen proto, ze se shoduje slovo.
Seznam slov se da ziskat snadno, pres striptags, tusim. Pripadne jednoduchym reg. vyrazem na odstraneni tagu. S im nahrazenim je to pak horsi, no.

No, mozna, zcela omezene, by se dali pouzit i vyrazy. Nahradil bych vse nezadouci za nejakou svou zkratku a pak tam dosadil zpet

<!DOCTYPE html>
<html>
    <head>	          
        <title>Řezací plotr | UV STAR</title>	          
        <meta charset="win-1250" />
    </head>
    <body>
<?php
@ini_set('error_reporting', E_ALL);
@ini_set("display_errors", "1");
error_reporting(E_ALL);

$html = '<p>materiály <a href="asdf" title="materiály"> asdf materiály asdf</a> asdf asdf materiály asd <strong>materiály</strong> <img alt="asdf materiály asdf " src="/www/images/zoomovatko.jpg" height="30" /></p>';

echo 'Originalni text';
echo '<hr>'.$html;
echo '<hr>'.htmlspecialchars($html); // proc mi to nefunguje?

$patterns = array();
$replacements = array();        
       
$links =  array((object) array('word' => 'materiály', 'anchor' => '/produkty/' , 'title' => 'title'));
foreach ($links AS $link) {
     
//   $patterns[]     = '/([^\"!\/!])('.$link->word.')([$\s\.\<])/i';
   $patterns[]     = '/([^\"!\/!])('.$link->word.')([$\s\.\<])/i';
/* preg_replace('~(<.*?>)(.*)?<~', '$1 <a href="$2">$2</a> <') */
   $replacements[]  = '$1<a href="' . $link->anchor . '" title="' . $link->title . '">$2</a>$3';

} 
         
echo 'Novy text';            
$new = $html;
$list = array();
$new = preg_replace_callback(
        	'~(\<(a) .*?(\>).*\<\/\2?\>)|(\<(img) .*?\>)~i',
        	function ($matches) {
			global $list;
			$list[] = $matches[0];
			return '#'.(count($list)-1);
		        },
		$new);
var_dump($list);
echo '<hr>'.$new;
$new = preg_replace($patterns, $replacements, $new);
$new = preg_replace_callback(
        	'~\#(\d+)~',
        	function ($matches) {
			global $list;
			$list[] = $matches[0];
			return $list[$matches[1]*1];
		        },
		$new);
echo '<hr>'.$new;

//echo preg_replace($patterns, $replacements, $html);

echo 'Takhle by mel vypadat';            
echo '<p><a href="/produkty/" title="title">materiály</a> <a href="asdf" title="materiály"> asdf materiály asdf</a> asdf asdf <a href="/produkty/" title="title">materiály</a> asd <strong><a href="/produkty/" title="title">materiály</a></strong> <img alt="asdf materiály asdf " src="/www/images/zoomovatko.jpg" height="30" /></p>';

/*
$doc = DOMDocument::loadHTML($string);
$xpath = new DOMXPath($doc);

$query = "//body/text()";
$nodes = $xpath->query($query);

$text = "";
foreach($nodes as $node) {
    $text .= $node->wholeText;
}    
*/

?>
</body>
</html>
peter
C / C++ › generator kodu
9. 2. 2016   #208502

BKX9XLGWR3
ZD7X9JDHGX
JSZPNRT9ZH
RD797X4K7M

Tak, nejdriv si zjistim rozsah hodnot, pocet moznych znaku. Teoreticku je to 0-9,A-Z. Pak je treba zjistit, zda jsou nektere znaky zparovane, treba u hexa zapisu (base16, #1F). Nebo muzes zkusit rovnou prevest na cislo. Nebo by mohlo jit o zapis base64. Pokud je to posloupnost, tak je treba zjistit rozdily mezi cisly. TTreba ty cisla jsou zapis md5/sha1/,,,, prvnich n znaku zakodovanych do 0-9A-Z (treba base36(md5(n)), n=0,1,2,...).
Jinymi slovy, bez znalosti mozne slozitosti a vzoru moznych algoritmu, to muze byt cokoliv.

Kdybych to bral treba znak po znaku, tak vzdalenost BZ je -25, ZJ je -16, JR je +8.

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý