Closed michael-ji0406 closed 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.
I can't succeefully use this lib。 This is my network. the echo is ws connection.
Below is the header of the connection.
// 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.
I used the namespace. ws://localhost:3000/game/echo can't access to echo router of game.js.
What is the solution for this issue. I am getting similar issue.
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:
@bluemir Could you provide the full code that creates the app and server? I'm having this same anomaly.
As my app uses Meteor, I was able to use one of the excellent Meteor/Apollo npm packages for this -- Swydo/ddp-apollo.
@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.
Hi, I have a trouble use this lib.
I can successfully get
localhost:3000/
and post data tolocalhost: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.