2017-02-19 67 views
5

WS.js我只有這在我的服務器上的文件和錯誤出現:「類型錯誤:這是不是一個類型的array.with」中的Node.js

const WebSocket = require('ws'); 

const wss = new WebSocket.Server({ port: 9000 }); 

wss.broadcast = function broadcast(data) { 
    wss.clients.forEach(function each(client) { 
    if (client.readyState === WebSocket.OPEN) { 
     client.send(data); 
    } 
}); 
}; 

wss.on('connection', function connection(ws) { 
    ws.on('message', function incoming(data) { 
     wss.broadcast(data); 
    }); 
}); 

器上顯示此錯誤:

/home/mgmaip/server/node_modules/ws/lib/PerMessageDeflate.js:8 
const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]); 
        ^

TypeError: this is not a typed array. 
    at Function.from (native) 
    at Object.<anonymous> (/home/mgmaip/server/node_modules/ws/lib/PerMessageDeflate.js:8:24) 
    at Module._compile (module.js:397:26) 
    at Object.Module._extensions..js (module.js:404:10) 
    at Module.load (module.js:343:32) 
    at Function.Module._load (module.js:300:12) 
    at Module.require (module.js:353:17) 
    at require (internal/module.js:12:17) 
    at Object.<anonymous> (/home/mgmaip/server/node_modules/ws/lib/WebSocket.js:16:27) 
    at Module._compile (module.js:397:26) 

我有節點的最後一個版本,有人知道會發生什麼? 謝謝!

回答

11

this issuews v2不適用於節點< 4.5.0(請參閱下面的兼容性)。你可以升級到節點的新版本或使用ws[email protected]以前的版本:

npm install [email protected] 

進行一些測試後,這裏是兼容性列表,節點v4.5.0 & v4.7.3之間,並從v5.10.0[email protected]作品:

| node version | [email protected] working | 
|--------------|--------------| 
| v4.4.7  |    | 
| v4.5.0  |  OK  | 
| v4.6.0  |  OK  | 
| v4.6.1  |  OK  | 
| v4.6.2  |  OK  | 
| v4.7.0  |  OK  | 
| v4.7.1  |  OK  | 
| v4.7.2  |  OK  | 
| v4.7.3  |  OK  | 
| v5.0.0  |    | 
| v5.1.0  |    | 
| v5.1.1  |    | 
| v5.2.0  |    | 
| v5.3.0  |    | 
| v5.4.0  |    | 
| v5.4.1  |    | 
| v5.5.0  |    | 
| v5.6.0  |    | 
| v5.7.0  |    | 
| v5.7.1  |    | 
| v5.8.0  |    | 
| v5.9.0  |    | 
| v5.9.1  |    | 
| v5.10.0  |  OK  | 
| v5.10.1  |  OK  | 
| v5.11.0  |  OK  | 
| v5.11.1  |  OK  | 
| v5.12.0  |  OK  | 
+0

我有節點版本:V5.4.1 – Fr33d0m

+0

API說這種方法添加V5.10 https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_buffer –

+1

我已經更新節點版本兼容的列表 –

相關問題