[英]Cannot use Socket.io 2.0.3
在開發視頻聊天應用程序的情況下,我嘗試使用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');
});
的index.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。舊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.