Skip to content

Instantly share code, notes, and snippets.

@michaelcontento
Created December 9, 2015 11:00
Show Gist options
  • Save michaelcontento/b1b9d7cf18b8c30f6bad to your computer and use it in GitHub Desktop.
Save michaelcontento/b1b9d7cf18b8c30f6bad to your computer and use it in GitHub Desktop.
var socket = require('socket.io-client')('http://127.0.0.1:3000');
socket.on('connect', function(io){
console.log('[CLIENT] connected');
var data = { name: 'hans' };
console.log('[CLIENT] sending json:', '(' + typeof data + ')', data);
socket.emit('json', data);
var data = 'hello world';
console.log('[CLIENT] sending string:', '(' + typeof data + ')', data);
socket.emit('string', data);
var data = 'client-ack-data';
console.log('[CLIENT] sending client-ack:', '(' + typeof data + ')', data);
socket.emit('client-ack', data, function (data) {
console.log('[CLIENT] got client-ack:', '(' + typeof data + ')', data);
var data = { from: 'client', value: 'ack-data' };
console.log('[CLIENT] sending client-ack-json:', '(' + typeof data + ')', data);
socket.emit('client-ack-json', data, function (data) {
console.log('[CLIENT] got client-ack-json:', '(' + typeof data + ')', data);
});
});
socket.on('server-ack', function(data, ack) {
console.log('[CLIENT] got server-ack:', '(' + typeof data + ')', data);
var respo = 'C-' + (Math.random() * 1000);
console.log('[CLIENT] sending server-ack response:', '(' + typeof respo + ')', respo);
ack(respo);
});
});
socket.on('event', function(data){
console.log('[CLIENT] event', data);
});
socket.on('disconnect', function(){
console.log('[CLIENT] disconnected');
});
{
"dependencies": {
"socket.io": "^1.3.7",
"socket.io-client": "^1.3.7"
}
}
var io = require('socket.io')();
io.on('connection', function(socket) {
console.log('\n\n');
console.log('[SERVER] client connected');
socket.on('json', function(data) {
console.log('[SERVER] got json:', '(' + typeof data + ')', data);
});
socket.on('string', function(data) {
console.log('[SERVER] got string:', '(' + typeof data + ')', data);
});
socket.on('client-ack', function(data, ack) {
console.log('[SERVER] got client-ack:', '(' + typeof data + ')', data);
var respo = 'S-' + (Math.random() * 1000);
console.log('[SERVER] sending client-ack response:', '(' + typeof respo + ')', respo);
ack(respo);
});
socket.on('client-ack-json', function(data, ack) {
console.log('[SERVER] got client-ack-json:', '(' + typeof data + ')', data);
var respo = { from: 'server', value: Math.random() * 1000 };
console.log('[SERVER] sending client-ack-json response:', '(' + typeof respo + ')', respo);
ack(respo);
setTimeout(function() {
console.log('[SERVER] sending server-ack');
socket.emit('server-ack', 'server-ack-data', function(data) {
console.log('[SERVER] got server-ack:', '(' + typeof data + ')', data);
});
}, 50);
});
});
io.listen(3000);
console.log('[SERVER] listening on 127.0.0.1:3000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment