2017-03-02 65 views
-1

我試圖讓WebSockets的連接去,我不斷收到以下錯誤: WebSocket connection to 'ws://localhost:3000/socket.io/?EIO=3&transport=websocket&sid=w6H2vNXXbYIOhwPQAAAA' failed: Error during WebSocket handshake: Unexpected response code: 400錯誤:400

我的代碼是非常簡單 - 在客戶端我打電話this.socket = io();和在服務器端我正在做下面的事情。我已經注意到這個主題上的幾個相關的帖子,並嘗試了很多變化,但似乎沒有任何工作。感謝提前:)

const app = require("http").createServer(handler); 
const io = require("socket.io")(app); 

app.listen(3000); 

class Sockets { 
    constructor() { 
    this.humanPlayers = []; 
    this.socket = io.listen(app); 
    this.setEventHandlers(); 
    } 
} 

回答

0

你需要監聽的套接字連接,然後就可以收到一個從Socket.io後創建一個新的Sockets實例。然後,您可以將從Socket.io收到的套接字存儲到新的Socketssocket屬性中。

const app = require("http").createServer(handler); 
const io = require("socket.io")(app); 

class Sockets { 
    constructor(socket) { 
    this.humanPlayers = []; 
    this.socket = socket; 
    this.setEventHandlers(); 
    } 
} 

app.listen(3000); 

io.on('connection', (socket) => { 
    let connectedSocket = new Sockets(socket); 
});