Created
November 4, 2014 21:44
-
-
Save chimmelb/aed15468b9051e35b4d5 to your computer and use it in GitHub Desktop.
engine.io-client connect to primus server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var socket = require( 'engine.io-client' )( 'http://localhost:8080' ); | |
| //engine.io-client callbacks | |
| socket.on( 'open', function() { | |
| console.log( 'open!' ); | |
| } ); | |
| socket.on( 'message', function( data ) { | |
| console.log( 'message=' + message.toString() ); | |
| } ); | |
| socket.on( 'close', function() { | |
| console.log( 'closed!!' ); | |
| } ); | |
| socket.on( 'error', function( err ) { | |
| console.log( 'error! ' + err ); | |
| } ); | |
| socket.on( 'flush', function() { | |
| console.log( 'flushed!!' ); | |
| } ); | |
| socket.on( 'drain', function() { | |
| console.log( 'drained!' ); | |
| } ); | |
| socket.on( 'upgradeError', function() { | |
| console.log( 'upgrade error!' ); | |
| } ); | |
| socket.on( 'upgrade', function() { | |
| console.log( 'upgraded!' ); | |
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Crazy-fast output! | |
| ## Server output | |
| Listening on port 8080 | |
| Handling http message. "/engine.io/?EIO=3&transport=polling&t=1415135906922-62&b64=1" | |
| Handling http message. "/engine.io/?EIO=3&transport=polling&t=1415135906923-63&b64=1" | |
| Handling http message. "/engine.io/?EIO=3&transport=polling&t=1415135906925-64&b64=1" | |
| Handling http message. "/engine.io/?EIO=3&transport=polling&t=1415135906927-65&b64=1" | |
| ... | |
| ## Client output | |
| error! Error: server error | |
| error! Error: server error | |
| error! Error: server error | |
| error! Error: server error | |
| ... | |
| ## Client output with `transports: ['websocket']`, after long timeout | |
| error! Error: websocket error | |
| closed!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var stringify = require( 'json-stringify-safe' ); | |
| var Primus = require( 'primus' ), | |
| http = require( 'http' ); | |
| var server = http.createServer( function( req, res ) { | |
| console.log( 'Handling http message. ' + stringify( req.url ) ); | |
| res.writeHead( 200, { | |
| 'Content-Type': 'application/javascript' | |
| } ); | |
| res.write( 'Hello' ); | |
| res.end(); | |
| } ); | |
| var primus = new Primus( server, { | |
| // authorization: null, | |
| pathname: '/primus', | |
| parser: 'JSON', | |
| transformer: 'engine.io', | |
| // plugin: {}, | |
| // timeout: 35000, | |
| //origins: '*', | |
| //methods: [ 'GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'OPTIONS' ], | |
| // credentials: true, | |
| // maxAge: '30 days', | |
| // exposed: false, | |
| } ); | |
| primus.on( 'connection', function( spark ) { | |
| console.log( 'connection: ' + spark.id ); | |
| spark.on( 'data', function boop( data ) { | |
| console.log( 'received ' + JSON.stringify( data ) + ' from ' + spark.id + ', sending boop' ); | |
| spark.write( { | |
| event: 'boop', | |
| data: 'bop' | |
| } ); | |
| } ); | |
| } ); | |
| primus.on( 'disconnection', function( spark ) { | |
| // the spark that disconnected | |
| console.log( 'disconnection: ' + spark.id ); | |
| } ); | |
| var port = 8080; | |
| server.listen( port, function() { | |
| console.log( 'Listening on port ' + port ); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment