Node.js 的 WebSocket 模块 ws.io
本文导语: A simple wrap to node.js ws module and then we can use it in a manner like socket.io. It's really easy to use socket.io but it doesn't support Blob yet. So I write a simple wrap to ws module for this purpose. This is also a part of works from a contest by ithelp.ithome.com.tw. Features: t...
A simple wrap to node.js ws module and then we can use it in a manner like socket.io. It's really easy to use socket.io but it doesn't support Blob yet. So I write a simple wrap to ws module for this purpose. This is also a part of works from a contest by ithelp.ithome.com.tw.
Features:trnasfer Blob/ArrayBuffer by put it in an object and emit it just as socket.io
broadcast to all but not me
emit to specified peer by socket.to(socket.id).emit('event', data)
join/leave/in room
in-memory store
can only send one Blob/ArrayBuffer within one emit
the Blob/ArrayBuffer object must be a property of the emitting object in the first level, no deeper
no configuration support
no 3rd party data store support
cannot scale (due to current sockets management and store design)
client support is now through a static url: /ws.io/ws.io.js
npm install ws.iousage
a simple echo server. (echo.js)
var app = require('http').createServer(handler), io = require('./ws.io').listen(app); app.listen(8443); function handler (req, res) { res.setHeader('Content-Type', 'text/html'); res.writeHead(200); res.end( ""+ ""+ ""+ ""+ ""+ "var socket = io.connect('ws://localhost:8443');"+ "socket.on('echo', function(data) {"+ " alert(data);"+ "});"+ ""+ ""+ "echo"+ ""+ ""+ ""+ "var button = document.getElementById('echo');"+ "button.onclick = function() {"+ " socket.emit('echo', 'hello echo server.');"+ "}"+ "" ); } io.sockets.on('connection', function (socket) { socket.on('echo', function(data) { socket.emit('echo', data); }); });
a simple blob sharing through file api. (blob.js)
var fs = require('fs'), url = require('url'), app = require('http').createServer(function(req, res) { res.setHeader('Content-Type', 'text/html'); res.writeHead(200); res.end( ""+ ""+ ""+ ""+ ""+ "#panel {"+ " border: solid 1px #336699;"+ " line-height: 20px;"+ " vertical-align: middle;"+ " padding: 5px;"+ " border-radius: 5px;"+ "}"+ ""+ ""+ ""+ ""+ "
"+ "