【发布时间】:2013-01-05 14:22:15
【问题描述】:
我目前正在使用 Websockets。
通过查看一些活跃的项目/实现,例如 einaros/ws(以及其他),我发现他们自己实现了服务器。而不是使用提供 tcp 服务器的 node net 模块。这种方法有原因吗?
https://github.com/einaros/ws/blob/master/lib/WebSocketServer.js
问候
更新:
var server = net.createServer(function(c) {
c.on('data', function(data) {
// data is a websocket fragment which has to get parsed
});
// transformToSingleUtfFragment is building a websocket valid
// byte fragment which contains hello as application payload
// and sets the right flags so the receiver knows we have a single text fragment
c.write(transformToSingleUtfFragment('hello'));
c.pipe(c);
});
server.listen(8124, function() { //'listening' listener
console.log('server bound');
});
【问题讨论】: