C#建立WAMP通讯

Server端,主程序中

static void Main(string[] args)
        {
            if (args.Any() && (args[0].ToLower() == "-c"))
            {
                if (ErrorCode.RestartService == ServiceCore.Default.Initialize())
                {
                    Logger.Warn("Initialize failed, need restart system.");
                    return;
                }
                ServiceCore.Default.Run();  //建立WAMP通讯
                Console.ReadKey();

2、Run()函数中存在

#region 启动WAMP Server
        ISentryRpcMethod SentryRpcMethod = SentryRpcProcessor.Default;
        ServerConnector.Default.Start(SentryRpcMethod);    
        #endregion

3、Start()函数中存在

    public void Start(ISentryRpcMethod rpcMethod)
        {
            try
            {
                m_wampHost = new DefaultWampHost(m_sServerURI);  
//传入服务端信息,初始化时已设定了m_sServerURI
                m_wampHost.SessionClosed += new EventHandler<WampSessionEventArgs>(OnSessionClosed);
                m_wampHost.SessionCreated += new EventHandler<WampSessionEventArgs>(OnSessionCreated);
                m_wampHost.HostService(rpcMethod);

                m_wampHost.Open();   //创建了服务端

                this.InitialTopics();  //初始化Topic
            }
//初始化时,初始化服务端IP与端口

public void Initialize(string sLocalAddr, int iPort)
   {
            m_sServerURI = "ws://" + sLocalAddr + ":" + iPort.ToString() + "/";

            Logger.Info("Communicate Server on " + m_sServerURI + " Initialized Completely~~~");
        }

Client端
1、点击登录按钮连接

if (!string.IsNullOrWhiteSpace(server))
                {
                  bool ret = CommonServiceMgr.Instance.ConnetService(server, 51008);
            return false; }

2、ConnectService()含有

if (!CommonClient.Instance.ReOpen())
 {
   Logger.Error(string.Format("Reopen server failed: ip = {0}, port = {1}", IPAddress, Port));  //增加此步骤,是为验证输入IP与Port是否正确,无此需求可直接与下一步合并

3、

public bool ReOpen()
        {
            try
            {
                m_channel.Close();
            }
            catch
            {
                // ignored
            }
  		  try
            {
                m_channel = channelFactory.CreateChannel("ws://" + _serverAddr + ":" + _serverPort + "/");

                m_channel.Open();

            m_RpcProxy = m_channel.GetRpcProxy<ISentryRpcMethod>();

            IWampClientConnectionMonitor monitor = m_channel.GetMonitor();
            monitor.ConnectionLost += new EventHandler(ConnectionLost);

            _sessionID = monitor.SessionId;  //服务端自动给配一个随机sessionID
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值