javascript - NodeJS and web sockets: Check if socket origin is the same as the web socket server -
edit: i'm sending data app app b on web socket, router app else, , b web app. when b receives data, should send client viewing home page, on web socket. but, since roter app , home page clients connected same web socket server, don't know connections clients viewing home page, , connections other stuff, router. home page clients should receive data.
i want pass logging data recieved router home page in real time can view it.
========
i have express app server simple html page. runs script:
var host = window.document.location.host.replace(/:.*/, ''); var ws = new websocket('ws://' + host + ':5000'); ws.onmessage = function (event) { console.log(json.parse(event.data)); };
in nodejs backend, have simple ws server running, listening connections:
var websocketserver = require("ws").server; module.exports.init = function(server) { var wss = new websocketserver({ server: server }); wss.on('connection', function (ws) { ... }); };
i connections from, @ moment, 2 different locations.
- a router app have running sending web app logging messages.
- the html-page web app serving.
i want pipe data router app html page, need know of connections need pipe data to. can in theory have many connection, 1 of them, @ least now, should passed data after recieved.
i thought compare origin of web socket connection domain of web server web socket server ran on.
i can origin of connection this: ws.upgradereq.headers.origin
. return e.g: localhost:5000
. don't know domain name web socket server running. i've tried google, , seems domain name, need http request. looking gives me name, without having wait http request.
i've tried os.hostname()
, doesn't give me results need. i've tried server.address()
, server
var server = require("http").createserver(app);
, gives me this: { address: '::', family: 'ipv6', port: 5000 }
.
isn't there way host , port? can somehow use address
part above host name?
the web app run on heroku.
based on recent comments, sounds each browser client connects websocket should tell server web page looking @ initial message , server should keep track of each active connection.
in socket.io (built on top of websockets), connect /homepage
namespace , server broadcast sockets connected namespace. could, of course, implement type of functionality plain websocket.
then, server not have list of connected sockets, know page from. allow broadcast based on current page. server-to-server websocket not have sent message it's home page, not tagged such , avoid sending it.
you might find socket.io easier use of this. in additon namespaces on both client , server, gives automatic reconnection browsers, simpler message passing system, server-side broadcast namsepaces , on.
Comments
Post a Comment