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
}