简体   繁体   English

无法使用Socket.io 2.0.3

[英]Cannot use Socket.io 2.0.3

In the case of developing an videochat app I try to use socket.io. 在开发视频聊天应用程序的情况下,我尝试使用socket.io。 To introduce myself to that library I followed differents tutorials, badly I always have the same problem. 为了向自己介绍该库,我遵循了不同的教程,很糟糕,我总是遇到相同的问题。

Each time I try to call the library like: 每次我尝试像这样调用库:

var io = require('socket.io')(http);

I have a syntax issue like : 我有一个语法问题,例如:

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 $ 

So I decide to use the previous version like socket.io 1.7.3 which works like a charm. 因此,我决定使用像socket.io 1.7.3这样的早期版本,它的工作原理就像一个魅力。 I just want to know if it's a common issue or not, and if someone know a way to solve it. 我只想知道这是否是常见问题,以及是否有人知道解决方法。

Thanks by advance 预先感谢

Here my code for that example : index.js 这是我的示例代码: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 的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM