Skip to content

Instantly share code, notes, and snippets.

@chimmelb
Last active August 29, 2015 14:08
Show Gist options
  • Save chimmelb/69da9a4a5ac34441ba26 to your computer and use it in GitHub Desktop.
Save chimmelb/69da9a4a5ac34441ba26 to your computer and use it in GitHub Desktop.
Socket.io server and client, connect to primus
var socket = require( 'socket.io-client' )( 'http://localhost:4567' );
//var socket = require( 'socket.io-client' )( 'http://127.0.0.1:8080/primus', {
// transports: [ 'polling', 'websocket' ],
// forceBase64: true,
// rememberUpgrade: false,
// enablesXDR: false,
// timestampRequests: true
//} );
//var socket = require( 'socket.io-client' )( 'http://127.0.0.1:8080/primus' );
socket.on( 'connect', function() {
console.log( 'connected!' );
} );
socket.on( 'error', function( err ) {
console.log( 'error ' + err.toString() );
} );
socket.on( 'disconnect', function() {
console.log( 'disconnected!' );
} );
socket.on( 'reconnect', function( num ) {
console.log( 'reconnected ' + num + ' times' );
} );
socket.on( 'reconnect_attempt', function() {
console.log( 'attempting to reconnect' );
} );
socket.on( 'reconnecting', function( num ) {
console.log( 'reconnecting ' + num + ' times' );
} );
socket.on( 'reconnect_error', function( err ) {
console.log( 'error reconnecting ' + err.toString() );
} );
socket.on( 'reconnect_failed', function() {
console.log( 'reconnect failed' );
} );
socket.on( 'boop', function( data ) {
console.log( 'Received ' + JSON.stringify( data ) );
socket.close();
} );
socket.emit( 'beep', {
event: 'beeper'
} );
## When it works
connected!
Received {"message":"here's a response","mirror":{"event":"beeper"}}
disconnected!
## When trying to connect to primus server
attempting to reconnect
reconnecting 1 times
error reconnecting Error: xhr poll error
attempting to reconnect
reconnecting 2 times
error reconnecting Error: xhr poll error
var io = require( 'socket.io' )( {
transports: [ 'websocket' ],
} );
io.attach( 4567 );
io.on( 'connection', function( socket ) {
console.log( 'socket connection made ' + socket.id );
socket.on( 'beep', function( data ) {
console.log( 'Received beep from socket ' + socket.id + ', returning boop' );
socket.emit( 'boop', {
message: "here's a response",
mirror: data
} );
} );
} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment