A
A
agentx0012014-02-28 23:07:50
JavaScript
agentx001, 2014-02-28 23:07:50

Why doesn't WebSocket connect to the server?

Actually there is a server on SockJS, when connecting, the client tells me that: WebSocket is closed before the connection is established. And although everything works fine with Loong Poling, I would like to connect via WebSocket because the delay is critical in this case.
Server code:

var 	http = require('http'),
  connect = require('connect'),
  sockjs = require('sockjs'),

...

var app = connect()
  .use(connect.static('./client'));

var server = http.createServer(app);	


var echo = sockjs.createServer({
  log: function(severity, line) {
        return log.log(severity, line);
    },
    websocket: true
});

echo.installHandlers(server, {
  prefix:'/socket-server'
});

echo.on('connection', function(conn) {
  ...
});


server.listen(config.get('port'));

Client code:
var sock = new SockJS('http://localhost:1000/socket-server');
...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Harakhnin, 2014-03-01
@Chupokabr

Here you need to study the issue with sockets. The fact is that Loong Poling works over the http protocol, so everything is ok. Web sockets send a request using the ws protocol. those. instead of address localhost:1000/socket-server, it should be ws://localhost:1000/socket-server
Better look at a href=" learn.javascript.ru/nodejs-screencast ">Ilya Kontor's Node js lessons. More specifically Part 2, Lesson 10.

D
Dmitry Kuznetsov, 2015-11-12
@dima9595

As I understand it, you made a socket client. You need to create a socket server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question