Skip to content

Instantly share code, notes, and snippets.

@thehawk970
Created July 23, 2014 09:27
Show Gist options
  • Save thehawk970/9c601fb14ee003ac7896 to your computer and use it in GitHub Desktop.
Save thehawk970/9c601fb14ee003ac7896 to your computer and use it in GitHub Desktop.
{
"name": "thehawk/ws-mwe-bug",
"require": {
"hoa/websocket": "dev-master"
},
"authors": [
{
"name": "thehawk",
"email": "thehawk@hoa-project.net"
}
],
"minimum-stability": "dev"
}
<input type="text" id="input" placeholder="Message" />
<hr />
<pre id="output"></pre>
<script>
var host = 'ws://127.0.0.1:8889';
var socket = null;
var input = document.getElementById('input');
var output = document.getElementById('output');
var print = function ( message ) {
var samp = document.createElement('samp');
samp.innerHTML = message + '\n';
output.appendChild(samp);
return;
};
input.addEventListener('keyup', function ( evt ) {
if(13 === evt.keyCode) {
var msg = input.value;
if(!msg)
return;
try {
socket.send(msg);
input.value = '';
input.focus();
}
catch ( e ) {
console.log(e);
}
return;
}
});
try {
socket = new WebSocket(host);
socket.onopen = function ( ) {
print('connection is opened');
input.focus();
return;
};
socket.onmessage = function ( msg ) {
print(msg.data);
return;
};
socket.onclose = function ( ) {
print('connection is closed');
return;
};
}
catch ( e ) {
console.log(e);
}
</script>
<?php
require 'vendor/autoload.php';
$server = new Hoa\Websocket\Server(
new Hoa\Socket\Server('tcp://127.0.0.1:8889')
);
$server->on('message', function ( Hoa\Core\Event\Bucket $bucket ) {
$data = $bucket->getData();
echo 'message: ', $data['message'], "\n";
$bucket->getSource()->broadcast($data['message']);
return;
});
$server->on('close', function ( Hoa\Core\Event\Bucket $bucket ) {
echo "connection closed\n";
$bucket->getSource()->broadcast("Un client s'est d├®connect├®");
return;
});
$server->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment