-
-
Save franklinjavier/377ac3f83aa673b1af5f to your computer and use it in GitHub Desktop.
This file contains 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 ws = require('websocket-stream') | |
var stream = ws('ws://localhost:8343') | |
stream.on('data', function(o) { | |
console.log("DATA", o) | |
}) | |
stream.write(new Buffer('hello')) |
This file contains 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
{ | |
"name": "socket-server", | |
"version": "1.0.0", | |
"description": "", | |
"main": "client.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "BSD", | |
"dependencies": { | |
"browserify": "^9.0.3", | |
"through2": "^0.6.3", | |
"websocket-stream": "^1.3.2", | |
"wzrd": "^1.2.1" | |
} | |
} |
This file contains 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 http = require('http') | |
var through = require('through2') | |
var websocket = require('websocket-stream') | |
var server = null | |
var port = 8343 | |
var url = 'ws://localhost:' + port | |
start(function(err) { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
console.log('Echo server started on port ' + port); | |
}); | |
function start(opts, cb) { | |
if (server) { | |
cb(new Error('already started')); | |
return; | |
} | |
if (typeof opts == 'function') { | |
cb = opts; | |
opts = {}; | |
} | |
server = http.createServer() | |
opts.server = server | |
websocket.createServer(opts, echo) | |
server.listen(port, cb) | |
function echo(stream) { | |
var logger = through(function(chunk, enc, next) { | |
console.log("message", chunk.length, 'bytes') | |
this.push(chunk) | |
next() | |
}) | |
stream.pipe(logger).pipe(stream) | |
} | |
} | |
function stop(cb) { | |
if (!server) { | |
cb(new Error('not started')) | |
return | |
} | |
server.close(cb) | |
server = null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment