Connect to specific route via socket.io

给你一囗甜甜゛ 提交于 2019-12-25 03:16:49

问题


I need to access Web sockets via specific path, I mount my socket.io on the client with a path ('ws') Server code:

var io = require('socket.io')(server, {path: '/notif'});

Client code:

var socket = io('//127.0.0.1:7733/ws/', {path: '/notif'});
socket.connect();

This does not work due to “ws” on client. I suspect it's because I don't have the equivalent on the server (e.g. require server on specific path). (when removing the /rt mount, everything seems to work as expected).

What is the server api to set up ws to listen on specific URL ?


回答1:


Are you sure you know what /ws/ in your url is used for?

Here you are asking to connect to the ws namespace. To receive connection for that namespace on the server you have to write:

io.of('/ws').on('connection', function(socket){
  console.log('someone connected');
});

See: http://socket.io/docs/rooms-and-namespaces/

Also you don't need to call socket.connect();

Calling io() or io.connect() will already try to establish connection with the server.



来源:https://stackoverflow.com/questions/26865884/connect-to-specific-route-via-socket-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!