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

推荐订阅源

V2EX - 技术
V2EX - 技术
P
Privacy International News Feed
Security Latest
Security Latest
H
Hacker News: Front Page
T
Tenable Blog
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Project Zero
Project Zero
O
OpenAI News
AI
AI
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
NISL@THU
NISL@THU
A
Arctic Wolf
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
U
Unit 42
S
Security Affairs
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
月光博客
月光博客
Engineering at Meta
Engineering at Meta
腾讯CDC
F
Full Disclosure
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
The Cloudflare 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项目。