Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit

  • Loading branch information...
commit 0c5afc9bcead1123a4e13a5e19fb9a1650ca63b5 0 parents
@stagas authored
0  README
No changes.
83 king.js
@@ -0,0 +1,83 @@
+var http = require('http')
+ , WebSocket = require('websocket')
+
+module.exports = function (opts) {
+ var king = new(require('events').EventEmitter)
+ , serverMode = false
+ , httpServer
+ , server
+ , client
+ , conn
+
+ king.send = function(msg, callback) {
+ serverMode
+ ? server.broadcastUTF(msg)
+ : conn.sendUTF(msg)
+ }
+ king.receive = function (callback) {
+ king.on('message', function (msg) {
+ callback(msg)
+ })
+ }
+
+ function log () {
+ opts.debug && console.log.apply(this, arguments)
+ }
+
+ ;(function connect () {
+ serverMode = !serverMode
+
+ function errorHandler (err) {
+ opts.debug > 1 && log('%s', err)
+ connect()
+ }
+
+ // Server mode
+ if (serverMode) {
+ opts.debug > 1 && log('\033[90mconnecting as \033[33mking\033[39m')
+ httpServer = http.createServer()
+ httpServer.on('error', errorHandler)
+ httpServer.listen(opts.port, opts.host, function () {
+ server = new WebSocket.server({
+ httpServer: httpServer
+ , autoAcceptConnections: true
+ })
+ server.on('error', errorHandler)
+ server.on('connect', function (c) {
+ opts.debug > 1 && log('\033[90mconnected: \033[37m%s', server.connections.length)
+ conn = c
+ conn.on('message', function (msg) {
+ server.connections.forEach(function (conn) {
+ if (conn !== c) conn.sendUTF(msg.utf8Data)
+ })
+ king.emit('message', msg.utf8Data)
+ })
+ conn.on('close', function (c) {
+ log('slave %s disconnected', conn.remoteAddress)
+ })
+ })
+ log('\033[33mconnected as king')
+ king.emit('connect', king)
+ })
+ }
+
+ // Client mode
+ else {
+ opts.debug > 1 && log('\033[90mconnecting as \033[37mslave\033[39m')
+ client = new WebSocket.client()
+ client.on('connectFailed', errorHandler)
+ client.on('connect', function (c) {
+ conn = c
+ conn.on('error', errorHandler)
+ conn.on('close', errorHandler)
+ conn.on('message', function (msg) {
+ king.emit('message', msg.utf8Data)
+ })
+ opts.debug > 1 && log('\033[90mconnected as \033[37mslave\033[39m')
+ king.emit('connect', king)
+ })
+ client.connect('ws://' + opts.host + ':' + opts.port)
+ }
+ }())
+ return king
+}
1  node_modules/websocket
@@ -0,0 +1 @@
+Subproject commit e00887723c55cf2f0b348d311c30db80b8c14e56
22 test-king.js
@@ -0,0 +1,22 @@
+var options = {
+ port: 4440
+, host: 'localhost'
+, debug: true
+, nick: process.argv[2] || require('Faker').Name.firstName()
+}
+
+process.stdin.resume()
+process.stdin.setEncoding('utf8')
+
+require('./king')(options).once('connect', function (conn) {
+ console.log('my nick is: %s', options.nick)
+ console.log('connected to network')
+ conn.receive(function (msg) {
+ console.log(msg)
+ })
+ ;(function broadcast (data) {
+ data = (data || '').split('\n')[0]
+ if (data && data.trim().length) conn.send(options.nick + ': ' + data)
+ process.stdin.once('data', broadcast)
+ }())
+})

2 comments on commit 0c5afc9

@jbaudanza

Not sure where you want to go with this, but I've been wanting a way that I can redeploy my websocket server without losing connections.

@stagas
Owner

I'm not sure either. It is an experiment, don't know of any real uses for this. Feel free to do it whatever.

Please sign in to comment.
Something went wrong with that request. Please try again.