在开发视频聊天应用程序的情况下,我尝试使用 socket.io。为了向那个库介绍自己,我遵循了不同的教程,很遗憾我总是遇到同样的问题。
每次我尝试像这样调用库时:
var io = require('socket.io')(http);
我有一个语法问题,如:
Projects/2017_07_24_firstSocketApp/node_modules/socket.io/node_modules/engine.io/node_modules/ws/lib/WebSocket.js:140
this._ultron.on('data', (data) => {
^
SyntaxError: missing ) after argument list
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:448:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/sandale/Documents/Projects/2017_07_24_firstSocketApp/node_modules/socket.io/node_modules/engine.io/node_modules/ws/index.js:9:19)
at Module._compile (module.js:430:26)
at Object.Module._extensions..js (module.js:448:10)
C02RN10GG8WN:2017_07_24_firstSocketApp $
所以我决定使用以前的版本,比如 socket.io 1.7.3,它就像一个魅力。我只想知道这是否是一个常见问题,以及是否有人知道解决方法。
提前致谢
这是我的示例代码:index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
索引.html
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
</body>
</html>
箭头函数 ( (data) => { ... }
) 仅在(相对)新版本的 Node.js(自 v4.5 起)中受支持,因此看起来您使用的 Node.js 版本太旧了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句