简体   繁体   English

apache的Socket.io-套接字客户端1 ID在连接客户端2时刷新

[英]Socket.io with apache - socket client 1 id refreshes on connecting client 2

I am using socket.io in my cakephp3 application for showing status of their processed requests to the connected clients. 我在我的cakephp3应用程序中使用socket.io来显示对已连接客户端的已处理请求状态。

Socket script (Server.js) : 套接字脚本(Server.js)

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var jsdom = require("jsdom");
const { JSDOM } = jsdom;

var dom = new JSDOM("");
var $ = require("jquery")(dom.window);

io.on('connection', function(socket){
  console.log('A user connected');

  socket.on('Event 1', function(data, fn){

    // Data received successfully!!!
    fn();
    // Processing
    sock.emit('Return 1', {data});
  });
  socket.on('disconnect', function(){
    console.log('A user disconnected');
  });
  socket.on('Event 2', function(data){
    // Processing
    var uid = data['uid']; //adding same user socket
    if(!sockets_uid[uid]){
        sockets_uid[uid] = [];
    }
    sockets_uid[uid].push(socket);
    $.each(sockets_uid, function(i, sock){
        sock.emit('Return 2', res);  
    });                 
});

http.listen(2105, function(){
    console.log('Started on 2105');
});

Client javascript(client.js) placed in default.ctp (CakePHP default rendering layout) : 客户端javascript(client.js)放在default.ctp(CakePHP默认呈现布局)中

var nodeToken = "<?= $this->request->session()->read('nodeToken');?>";
var socket = io('http://localhost/node', {secure: true, query: {token: nodeToken}});
socket.on('connect_error', function(){
    console.log('Unable to connect);
});

The workflow is working as expected when client is connecting to the node server via http://localhost:2105 . 客户端通过http://localhost:2105连接到节点服务器时,工作流按预期工作。

But on porting the code to production, I used apache(v2.4.7) to mask the port using Proxy Pass and rewrite to forward any requests to http://localhost/node . 但是在将代码移植到生产环境时,我使用apache(v2.4.7)使用Proxy Pass屏蔽了端口,并rewrite以将任何请求转发到http://localhost/node Apache2 config : Apache2的配置

Upon connecting to node via apache, initial connection is established successfully and client is receiving the responses from the node server as expected. 通过apache连接到节点后,初始连接成功建立,并且客户端正按预期从节点服务器接收响应。 As socket-client connection is being written in default.ctp, the connection to the node server refreshes on each page refresh or redirect to any other view using same default.ctp layout. 当在default.ctp中写入套接字-客户端连接时,到节点服务器的连接在每次刷新页面时刷新,或使用相同的default.ctp布局重定向到任何其他视图。

The problem I am facing is, as soon as some action is performed-say calling another any view ctp, the parent client didn't able to receive any update pushed by the node server to the client. 我面临的问题是,一旦执行了某些操作(例如调用另一个任意视图ctp),父客户端便无法接收节点服务器推送给客户端的任何更新。

If apache is not used and node server is directly connected with port in client.js then everything works as expected, so I suspect there must be some problem in apache2 proxy pass configuration. 如果不使用apache,并且节点服务器直接与client.js中的端口连接,那么一切都会按预期进行,因此我怀疑apache2 proxy pass配置中肯定存在一些问题。 I have already enabled mod_proxy and mod_ws_proxy modules. 我已经启用了mod_proxymod_ws_proxy模块。

Any help would be highly appreciated. 任何帮助将不胜感激。

After much googling and browsing, I found that after the page is reloaded or in my case, default.ctp layout is used for another view render, the socket connection is ungracefully terminated . 经过大量的搜索和浏览后,我发现在重新加载页面后,或者在我的情况下,将default.ctp布局用于另一个视图渲染, 套接字连接被非正常终止

So, I solved this problem by maintaining 3 arrays for holding: 因此,我通过维护3个数组来解决此问题:

  • Users => Sockets-IDSs 用户=>套接字IDS
  • Sockets-IDs => Sockets, and 套接字ID =>套接字,并且
  • All Active sockets 所有活动插座

Now, upon page refresh/socket disconnection, I removed the old terminated sockets and emit the event to the new sockets only. 现在,在页面刷新/套接字断开连接时,我删除了旧的终止套接字,并将事件仅发送到新套接字。

socket.on('disconnect', function(){
    var socket_id = socket.id;
    var uid = uids_sockets[socket_id];
    delete sockets[socket_id];
    var temp_array = sockets_uid[uid];
    var index = temp_array.indexOf(socket_id);
    temp_array.splice(index, 1);
    sockets_uid[uid] = temp_array;
  });

Also, on the client side, I am emitting the start event on socket connect event, so after new connection is established, the event gets re-registered with the new socket. 另外,在客户端,我在套接字连接事件上发出启动事件,因此在建立新连接后,该事件将重新注册到新套接字。

All the configuration is not required if connection to the node is established directly without using Apache Proxy-pass Middleware . 如果不使用Apache Proxy-pass Middleware而直接建立到节点的连接,则不需要所有配置。

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

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