HenningM / express-ws

WebSocket endpoints for express applications
BSD 2-Clause "Simplified" License
877 stars 142 forks source link

(index):20 WebSocket connection to 'ws://localhost:3000/so' failed: Connection closed before receiving a handshake response #98

Open ldc2726 opened 6 years ago

ldc2726 commented 6 years ago

`var express = require('express'); var app = express(); var expressWs = require('express-ws')(app);

app.use(function (req, res, next) { console.log('middleware'); req.testing = 'testing'; return next(); });

app.get('/', function(req, res, next){ console.log('get route', req.testing); res.end(); });

app.ws('/', function(ws, req) { console.log(111111111) ws.on('message', function(msg) { console.log(msg,'11111111'); }); console.log('socket'); });

module.exports = app; var ws = new WebSocket('ws://localhost:3000/so');

    ws.onopen = function(evt) {

        console.log(evt, '链接成功')

    };

    ws.onmessage = function(evt) {
        console.log(evt, '退消息')
        console.log("Received Message: " + evt.data);
        // ws.close();
    };`

Hello, there's nothing wrong with me using express+express-ws directly, but I've had trouble with express-cli with an address link.I built an so.js in the lie folder. The restApi is not a problem, but ws is not good. Please help us

BkunS commented 6 years ago

Had the same issue. Client is using "websocket"package:

import { w3cwebsocket as W3CWebSocket } from 'websocket';

const client = new W3CWebSocket('ws://localhost:10010/', 'echo-protocol');

client.onerror = function() {
  console.log('Connection Error'); // Triggered onerror.
}
Uday-Knetworks commented 3 years ago

am also facing same issue !!!!!

failed: Connection closed before receiving a handshake response