Socket.IO在具有Express.js vhost的子域上 [英] Socket.IO on subdomains with Express.js vhost

查看:134
本文介绍了Socket.IO在具有Express.js vhost的子域上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上有两个Express.js应用程序。



一个简单的香草应用程序称为主应用程序,另一个使用Socket.IO,名为socket -app。



我的主应用程序运行在mydomain.com& socket-app运行在socket.mydomain.com的子域上



我通过Express的内置vhost中间件将请求路由到套接字应用程序。 / p>

- main-app.js里面 -

  var express = require('express'); 
var app = module.exports = express.createServer();
app.use(express.vhost('socket.mydomain',require('./ socket-app / app.js')));

app.listen(8080,function(){
console.log(Express server listening in port%d in%s mode);
});

这工作正常,我可以看到我的socket应用程序在端口8080上运行在socket.mydomain



但是,如同讨论的 here。



所以我的问题是如何管道这个升级事件从我的主应用程序到我的套接字应用程序,所以所有连接的插座可以听到有人连接&断开连接?



我尝试从主应用程序中发出升级事件,但似乎没有起作用。

  app.on('upgrade',function(req,socket){
socket.emit('upgrade',app);
});

我在这里缺少什么?

解决方案

解决方案是在您的根应用程序中创建一个Socket.IO实例,可以由您的子域应用程序全局访问,然后在其自己的Socket.IO命名空间中运行每个应用程序,以保持其自主性



我刚刚构建了一个运行三个的简单示例聊天应用程序的实例都共享与根应用程序中声明的Socket.IO相同的实例。



如果您克隆项目,并按照说明设置本地子域,您可以看到独立于其他聊天应用程序的每个实例。



我将尝试写明博客文章,以及明天上午如何运作的更多细节。



更新



这是一个写如何在多个域中共享Socket.IO实例。


I have two Express.js apps running on my server.

A plain vanilla app called "main-app" and another that uses Socket.IO called "socket-app".

I have "main-app" running at "mydomain.com" & "socket-app" running on a subdomain at "socket.mydomain.com"

I am routing requests to the socket-app via Express's built-in vhost middleware.

-- inside main-app.js --

var express = require('express');
var app = module.exports = express.createServer();
app.use(express.vhost('socket.mydomain', require('./socket-app/app.js')));

app.listen(8080, function(){
  console.log("Express server listening on port %d in %s mode");
});

This works fine and I can see my socket-app running on port 8080 at socket.mydomain

However, there seems to be a problem with websockets timing out and not receiving the "upgrade" event when running a Socket.IO app through vhost as discussed here.

So my question is how can I pipe this "upgrade" event from my main-app to my socket-app so all connected sockets can hear when someone connects & disconnects?

I've tried emitting the "upgrade" event from within "main-app" but it doesn't appear to be working.

app.on('upgrade', function(req, socket) {
    socket.emit('upgrade', app);    
});

What am I missing here?

解决方案

The solution is to create one instance of Socket.IO in your root application that can be globally accessed by your subdomain applications and then run each app in its own Socket.IO namespace to preserve their autonomy.

I just built a simple example that runs three instances of a chat application all sharing the same instance of Socket.IO, which is declared in the root application.

If you clone the project and follow the instructions to set up the local subdomains you can see each instance of the chat application running independent of the others.

I will try to write up blog post with further details about how it works tomorrow morning.

Update

Here's a write up of how to share a Socket.IO instance across multiple domains.

这篇关于Socket.IO在具有Express.js vhost的子域上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆