This project is read-only.

Multiple server instances, and interactions among them

SuperSocket support running multiple server instances with different listening ip and port in the same program, by adding two server nodes in the configuration file:
<?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>

In some cases, interaction between two server instances is required. For example, client C1 of server A want to send message to client C2 of server B, so you need to forwarding the message through server B to the client C2 after server A receive the command from C1.

In OnStartup method of MyAppServerA, you can used method GetServerByName (string name) of SocketServerManager to get the server instance by 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);
        }
    }
}

In this way, you can send message to clients of server B from server A by calling server B's method.

Please checkout the full sample code from MultipleAppServer project of QuickStart in source code of SuperSocket.

Last edited Nov 23, 2011 at 1:34 AM by kerryjiang, version 4

Comments

No comments yet.