按照下面连接来搭建websocket。
新建了一个server.js文件
console .log( "Server started" );1. var Msg = '' ;1. var WebSocketServer = require ( 'ws' ).Server1. , wss = new WebSocketServer({ port : 8010 });1. wss.on( 'connection' , function ( ws ) {1. ws.on( 'message' , function ( message ) {1. console .log( 'Received from client: %s' , message);1. ws.send( 'Server received from client: ' + message);1. });1. });
node server.js 时候,出现如下错误。
Server started /root/node_modules/ws/lib/websocket.js:347 ...options ^^^ SyntaxError: Unexpected token ... at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:549:28) at Object.Module._extensions..js (module.js:586:10) at Module.load (module.js:494:32) at tryModuleLoad (module.js:453:12) at Function.Module._load (module.js:445:3) at Module.require (module.js:504:17) at require (internal/module.js:20:19) at Object. (/root/node_modules/ws/index.js:3:19)
https://www.oschina.net/translate/websocket-nginx