HenningM / express-ws

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

Error: Connection closed before receiving a handshake response #36

Closed michael-ji0406 closed 8 years ago

michael-ji0406 commented 8 years ago

Hi, I have a trouble use this lib.

// my route file
var express = require('express');
var expressWs = require('express-ws')(express());
var event = require('events').EventEmitter();
var app = expressWs.app;
/* GET users listing. */
app.get('/', function(req, res, next) {
    res.send('respond with a game');
});

app.post('/result', function(req, res, next) {
    console.log(req);
    event.emit('update_hp', req);
});

app.ws('/hp', function(ws, req) {
    console.log('connect ws');
    next();
});

module.exports = app;
//my client file
var liveSocket = new WebSocket("ws://localhost:3000/hp");
liveSocket.onclose(function(){
  console.log 'closed';
});

I can successfully get localhost:3000/ and post data to localhost:3000/result

but console output error:

WebSocket connection to 'ws://localhost:3000/hp' failed: Connection closed before receiving a handshake response

Can you help me? Thanks in advance.

roastduck commented 8 years ago

On line 18, you call next, which is undefined. This will throw an exception and your program terminated. Currently express-ws has something wrong with error handling, so the error did not prompt.

michael-ji0406 commented 8 years ago

I can't succeefully use this lib。 This is my network. the echo is ws connection.

net

Below is the header of the connection.

ws

//  routers/game.js
var express = require('express');
var router = express.Router();
require('express-ws')(router);

router.ws('/echo', function(ws, req) {
    console.log('establish connection');
});

module.exports = router;

// app.js
var express = require('express');
var expressWs = require('express-ws')(express());
var app = expressWs.app;
var game = require('./routes/game');
app.use('/game', game);

// bin/www, my starter file
var app = require('../app');
var http = require('http');
var expressWs = require('express-ws');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
expressWs = expressWs(app, server);
server.listen(port);
...

This is my client js code.

liveSocket = new WebSocket("ws://localhost:3000/game/echo")
liveSocket.onopen = ->
  console.log('has established connection')
  liveSocket.send('Can you hear me?')

liveSocket.onmessage = (event) ->
  console.log('ok')
  console.log(event.data)

liveSocket.onclose = ->
  console.log('will be closed')

The above will output "has established connection" and "will be closed". So I guess the connection is automatically closed. I don't know how to exchange messages.

I need help. Thanks in advance.

michael-ji0406 commented 8 years ago

I used the namespace. ws://localhost:3000/game/echo can't access to echo router of game.js.

yogeshsajanikar commented 7 years ago

What is the solution for this issue. I am getting similar issue.

bluemir commented 7 years ago

I have same problem, but, luckily, I found why it dosen't work. When use http.createServer(), must use expressWs(app, server); instead expressWs(app).

It's weird.... :disappointed_relieved:

VikR0001 commented 6 years ago

@bluemir Could you provide the full code that creates the app and server? I'm having this same anomaly.

VikR0001 commented 6 years ago

As my app uses Meteor, I was able to use one of the excellent Meteor/Apollo npm packages for this -- Swydo/ddp-apollo.

michael-ts commented 3 years ago

@bluemir This seems to be a chicken and egg situation for me. The modules that sets up the routes is called before a totally different module calls http.createServer, making it impossible to pass the server to expressWs.