express-ws icon indicating copy to clipboard operation
express-ws copied to clipboard

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

Open ldc2726 opened this issue 6 years ago • 2 comments

`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

ldc2726 avatar Jul 19 '18 10:07 ldc2726

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.
}

BkunS avatar Aug 01 '18 15:08 BkunS

am also facing same issue !!!!!

failed: Connection closed before receiving a handshake response

Uday-Knetworks avatar Feb 23 '21 10:02 Uday-Knetworks