惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
V
Vulnerabilities – Threatpost
T
Tenable Blog
Forbes - Security
Forbes - Security
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Visual Studio Blog
WordPress大学
WordPress大学
Latest news
Latest news
K
Kaspersky official blog
T
Tailwind CSS Blog
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
C
Cisco Blogs
博客园 - 聂微东
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
小众软件
小众软件
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
S
SegmentFault 最新的问题
Security Latest
Security Latest
Y
Y Combinator Blog
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
月光博客
月光博客
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
S
Security Affairs
P
Proofpoint News Feed
D
DataBreaches.Net
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG

博客园 - 江大渔

SuperSocket 2.0 发布第一个预览版, 另寻找Yang Fan哥哥 .NET Core 1.0 RC2 历险之旅 - 江大渔 使用LogMaster4Net实现应用程序日志的集中管理 SuperSocket 1.5 stable发布 在Mono/Linux上使用PerformanceCounter SuperSocket 1.5 文档列表 SuperWebSocket 0.5发布 WebSocket4Net 0.5发布 SuperWebSocket发布0.1版本 用ILMerge合并Silverlight, WindowsPhone或Mono for Android的程序集 SuperSocket 1.4 SP2 发布了! 无需等待,SuperSocket 1.4 SP1 发布了! SuperSocket 1.4 stable正式发布 SuperSocket 1.4系列文档(18) 在Unix/Linux操作系统中通过Mono运行SuperSocket SuperSocket 1.4系列文档(17) 在Windows Azure中运行SuperSocket SuperSocket 1.4系列文档(16) 在SuperSocket中启用传输层加密(TLS/SSL) SuperSocket 1.4系列文档(15) 在SuperSocket中启用内置Flash/Silverlight策略服务器 SuperSocket 1.4系列文档(13) 连接过滤器(Connection Filter) SuperSocket 1.4系列文档(12) 命令过滤器(Command Filter)
SuperSocket 1.4系列文档(14) 多服务器实例和服务器实例之间的交互
江大渔 · 2011-05-15 · via 博客园 - 江大渔

SuperSocket支持在同一程序中运行多个服务器实例监听在不同的IP或端口,只需在配置文件中添加两个server节点即可:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="socketServer" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/>
    </configSections>
    <appSettings>
        <add key="ServiceName" value="SuperSocket"/>
    </appSettings>
    <socketServer>
        <servers>
            <server name="ServerA"
                    serviceName="MyAppServerA"
                    ip="Any" port="911" mode="Async">
            </server>
            <server name="ServerB"
                    serviceName="MyAppServerB"
                    ip="Any" port="912" mode="Async">
            </server>
        </servers>
        <services>
            <service name="MyAppServerA"
                     type="SuperSocket.QuickStart.MultipleAppServer.MyAppServerA, SuperSocket.QuickStart.MultipleAppServer" />
            <service name="MyAppServerB"
                     type="SuperSocket.QuickStart.MultipleAppServer.MyAppServerB, SuperSocket.QuickStart.MultipleAppServer" />
        </services>
    </socketServer>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    </startup>
</configuration>

在某些情况下,需要两个服务器实例之间进行交互。例如ServerA上的客户端C1向ServerB上的客户端C2转发消息,这样就需要ServerA接收到C1的命令之后通过ServerB将消息转发给客户端C2。

在MyAppServerA的OnStartup方法里面可利用SocketServerManager的GetServerByName(string name)方法通过服务器实例的名称来获取服务器实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
using SuperSocket.SocketEngine;
 
namespace SuperSocket.QuickStart.MultipleAppServer
{
    public class MyAppServerA : AppServer
    {
        private IDespatchServer m_DespatchServer;
 
        protected override void OnStartup()
        {
            m_DespatchServer = SocketServerManager.GetServerByName("ServerB") as IDespatchServer;
            base.OnStartup();
        }
 
        internal void DespatchMessage(string targetSessionKey, string message)
        {
            m_DespatchServer.DispatchMessage(targetSessionKey, message);
        }
    }
 
    interface IDespatchServer
    {
        void DispatchMessage(string sessionKey, string message);
    }
 
    public class MyAppServerB : AppServer, IDespatchServer
    {
        public void DispatchMessage(string sessionKey, string message)
        {
            var session = GetAppSessionByIndentityKey(sessionKey);
            if (session == null)
                return;
 
            session.SendResponse(message);
        }
    }
}

这样就可以在MyAppServerA中通过调用MyAppServerB实例的方法来向ServerB的客户端发送消息了。

示例代码请参考SuperSocket源代码QuickStart中的MultipleAppServer项目。