简体   繁体   English

NodeJS和Web套接字:检查套接字来源是否与Web套接字服务器相同

[英]NodeJS and web sockets: Check if socket origin is the same as the web socket server

Edit: I'm sending data from app A to app B over web socket, where A is a router app for something else, and B is my web app. 编辑:我正在通过Web套接字将数据从应用程序A发送到应用程序B,其中A是其他应用程序的路由器应用程序,而B是我的Web应用程序。 When B receives the data, it should send it to any client viewing its home page, also over web socket. 当B接收到数据时,它也应该通过Web套接字将其发送给查看其主页的任何客户端。 But, since the roter app and the home page clients are connected to the same web socket server, I don't know which connections are to the clients viewing the home page, and which connections are to other stuff, like my router. 但是,由于轮流应用程序和主页客户端连接到同一Web套接字服务器,所以我不知道哪些连接是查看主页的客户端,哪些连接是其他路由器(例如路由器)。 Only the home page clients should receive the data. 仅主页客户端应接收数据。

I basically want to pass the logging data recieved from my router to my home page in real time so I can view it. 我基本上想将从路由器收到的日志记录数据实时传递到我的主页,以便可以查看它。

======== ========

I have an express app that server a simple html page. 我有一个快速应用程序,可以处理一个简单的html页面。 It runs this 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 the nodejs backend, I have a simple ws server running, listening for connections: 在nodejs后端,我有一个运行中的简单ws服务器,监听连接:

var WebSocketServer = require("ws").Server;

module.exports.init = function(server) {
  var wss = new WebSocketServer({ server: server });
  wss.on('connection', function (ws) {
    ...
  });
};

I get connections from, at the moment, 2 different locations. 目前,我从2个不同的位置获得连接。

  1. A router app I have running that is sending this web app logging messages. 我正在运行的路由器应用正在发送此Web应用日志消息。
  2. The html-page that this web app is serving. 此Web应用程序正在提供的html页。

I want to pipe the data from the router app to my html page, but to do that I need to know which of my connections I need to pipe the data to. 我想将数据从路由器应用程序传送到我的html页面,但是要做到这一点,我需要知道需要将数据传送到哪个连接。 I can in theory have many connection, but only one of them, at least for now, should be passed the data after it is recieved. 从理论上讲,我可以有许多联系,但至少在目前,只有其中之一应在收到数据后传递给数据。

I thought I could compare the origin of the web socket connection to the domain of the web server the web socket server ran on. 我以为可以将Web套接字连接的来源与运行Web套接字服务器的Web服务器的域进行比较。

I can get the origin of the connection like this: ws.upgradeReq.headers.origin . 我可以这样获得连接的起点: ws.upgradeReq.headers.origin That will return eg: localhost:5000 . 那将返回例如: localhost:5000 But I don't know the domain name where my web socket server is running. 但是我不知道我的Web套接字服务器运行所在的域名。 I've tried to google, and it seems like to get the domain name, I need to get it from an http request. 我尝试过使用google,似乎要获取域名,我需要从http请求中获取它。 What I am looking for is something that just gives me the name, without having to wait for an http request. 我正在寻找的只是给我起名字的东西,而不必等待http请求。

I've tried os.hostname() , but it doesn't give me the results I need. 我已经尝试过os.hostname() ,但是没有给我我想要的结果。 I've also tried server.address() , where server is var server = require("http").createServer(app); 我也尝试过server.address() ,其中servervar server = require("http").createServer(app); , but that gives me this: { address: '::', family: 'IPv6', port: 5000 } . ,但这给了我这个: { address: '::', family: 'IPv6', port: 5000 }

Isn't there just a way to get the host and port? 是否只有一种获取主机和端口的方法? Can I somehow use the address part above to get the host name? 我可以以某种方式使用上面的address部分来获取主机名吗?

The web app will probably run on Heroku. 该网络应用程序可能会在Heroku上运行。

Based on your recent comments, it sounds like each browser client that connects a webSocket should just tell your server what web page it is looking at with an initial message and the server should keep track of that for each active connection. 根据您最近的评论,听起来每个连接到WebSocket的浏览器客户端都应该使用初始消息告诉您的服务器正在查看哪个网页,并且服务器应该跟踪每个活动连接的页面。

In socket.io (built on top of webSockets), you could just connect to the /homepage namespace and then that server could broadcast to all sockets connected to that namespace. 在socket.io(基于webSockets构建)中,您可以仅连接到/homepage命名空间,然后该服务器可以广播到连接到该命名空间的所有套接字。 You could, of course, implement that type of functionality yourself with a plain webSocket. 当然,您可以使用简单的webSocket自己实现这种功能。

Then, your server would not only have a list of connected sockets, but could also know what page they were all from. 这样,您的服务器不仅将具有已连接套接字的列表,而且还可以知道它们全部来自哪个页面。 That would allow you to broadcast based on current page. 那将允许您基于当前页面进行广播。 Your server-to-server webSocket would not have sent a message that it's from the home page, so it would not be tagged as such and you could avoid sending to it. 服务器到服务器的webSocket不会发送来自主页的消息,因此不会被标记为此类,因此可以避免发送给它。

You might find socket.io easier to use for all of this. 您可能会发现socket.io更易于使用。 In additon to namespaces on both client and server, it also gives you automatic reconnection from browsers, a simpler message passing system, server-side broadcast to namsepaces and so on. 除了客户端和服务器上的名称空间外,它还使您可以从浏览器自动重新连接,更简单的消息传递系统,服务器端广播到别名分隔等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM