WebSocket-Node开源项目

我要开发同款
匿名用户2013年11月21日
135阅读

技术信息

开源地址
https://github.com/Worlize/WebSocket-Node
授权协议
Apache

作品详情

WebSocket-Node是对WebSocket协议实现的Node.js扩展。

服务器端示例代码:

#!/usr/bi/evodevarWebSocketServer=require('websocket').server;varhttp=require('http');varserver=http.createServer(fuctio(request,respose){cosole.log((ewDate())+'Receivedrequestfor'+request.url);respose.writeHead(404);respose.ed();});server.liste(8080,fuctio(){cosole.log((ewDate())+'Serverislisteigoport8080');});wsServer=ewWebSocketServer({httpServer:server,//YoushouldotuseautoAcceptCoectiosforproductio//applicatios,asitdefeatsallstadardcross-origiprotectio//facilitiesbuiltitotheprotocoladthebrowser.Youshould//*always*verifythecoectio'sorigiaddecidewhetherorot//toacceptit.autoAcceptCoectios:false});fuctioorigiIsAllowed(origi){//putlogicheretodetectwhetherthespecifiedorigiisallowed.returtrue;}wsServer.o('request',fuctio(request){if(!origiIsAllowed(request.origi)){//Makesureweolyacceptrequestsfromaallowedorigirequest.reject();cosole.log((ewDate())+'Coectiofromorigi'+request.origi+'rejected.');retur;}varcoectio=request.accept('echo-protocol',request.origi);cosole.log((ewDate())+'Coectioaccepted.');coectio.o('message',fuctio(message){if(message.type==='utf8'){cosole.log('ReceivedMessage:'+message.utf8Data);coectio.sedUTF(message.utf8Data);}elseif(message.type==='biary'){cosole.log('ReceivedBiaryMessageof'+message.biaryData.legth+'bytes');coectio.sedBytes(message.biaryData);}});coectio.o('close',fuctio(reasoCode,descriptio){cosole.log((ewDate())+'Peer'+coectio.remoteAddress+'discoected.');});});

功能介绍

WebSocket-Node 是对 WebSocket 协议实现的 Node.js 扩展。 服务器端示例代码: #!/usr/bin/env node
var WebSocketServer =...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论