Created
January 5, 2012 10:31
-
-
Save netroy/1564640 to your computer and use it in GitHub Desktop.
Connect with Websockets
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 connect = require('connect'), | |
WebSocketServer = require('websocket').server, | |
app = connect.createServer(); | |
app.use(connect.static(process.cwd())); | |
app.use(connect.router(function(app){ | |
app.get("/", function(req, resp) { | |
resp.write("Hola !"); | |
resp.end(); | |
}); | |
})); | |
wsServer = new WebSocketServer({ | |
httpServer: app, | |
autoAcceptConnections: false | |
}); | |
wsServer.on('request', function(request) { | |
var connection = request.accept('', request.origin); | |
connection.on('message', function(message) { | |
if (message.type === 'utf8') { | |
connection.sendUTF(message.utf8Data); | |
} | |
}); | |
connection.on('close', function(reasonCode, description) { | |
// Cleanup | |
}); | |
}); | |
if (!module.parent) { | |
app.listen(process.env.app_port || 8888); | |
} | |
console.log("Push server started"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment