Socket.io - failed: Connection closed before receiving a handshake response

前端 未结 5 1334
庸人自扰
庸人自扰 2021-01-01 15:24

Socket.io for NodeJS doesn\'t seem to work as a websocket server

For some reason, socket.io ALWAYS fallback to the long polling and if I force the w

5条回答
  •  一生所求
    2021-01-01 16:13

    Here is the complete working example from one of our working projects:

    const websocket = require('ws');
    const http = require('http');
    const express = require('express');
    const app = express();
    const server = http.createServer(app);
    wss = new websocket.Server({ server });
    app.on('upgrade', wss.handleUpgrade);
    
    wss.on('connection', ws => {
      console.log('web socket connection is alive');
    }
    
    server.listen(8080, () => {
        console.log('server started on PORT 8080');
    });
    

    translated from Medium

提交回复
热议问题