學習SuperSocket 必須要注意的 代碼是
static void Main(string[] args) { WebSocketServer appServer = new WebSocketServer(); if (!appServer.Setup(2000)) //端口 { Console.WriteLine("端口錯誤!"); Console.ReadKey(); return; } appServer.NewSessionConnected += new SessionHandler<WebSocketSession>(appServer_NewClientConnected); appServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived); appServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed); if (!appServer.Start()) { Console.WriteLine("啟動錯誤"); Console.ReadKey(); return; } Console.WriteLine("服務器啟動成功, 按 'q' 退出服務器!"); while (Console.ReadKey().KeyChar != 'q') { Console.WriteLine(); continue; } appServer.Stop(); Console.WriteLine(); Console.WriteLine("服務器關閉"); Console.ReadKey(); }
學習 unity 通訊 必須注意的代碼是
// 服務器IP地址和端口號 ServerAdress = "ws://192.168.1.104:2000/"; websocket = new WebSocket(ServerAdress); websocket.Open();
public void addlistenerSocket() { _SocketModel.websocket.Opened += new EventHandler(websocket_Opened); _SocketModel.websocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error); _SocketModel.websocket.Closed += new EventHandler(websocket_Closed); _SocketModel.websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived); }
//登錄 發送監聽 private void websocket_Opened(object sender, EventArgs e) { JsonData message = new JsonData(); message["Agreement"] = Agreement.user_no_return; message["username"] = _SocketModel.PlayName; _SocketModel.websocket.Send(message.ToJson());//這個是發到消息端 } //關閉監聽 public virtual void websocket_Closed(object sender, EventArgs e) { } //接收監聽 public void websocket_MessageReceived(object sender, MessageReceivedEventArgs e) { JsonData jsontext = JsonMapper.ToObject(e.Message); //轉化成json格式數據 string Agreements = jsontext["Agreement"].ToString(); switch (Agreements) { case Agreement.user_offline_return://接收到用戶下線通知 user_offline_return(jsontext); break; case Agreement.user_chat_return://接收聊天信息 user_chat_return(jsontext); break; case Agreement.user_battle_return://接收到對戰通知 user_battle_return(jsontext); break; case Agreement.user_information_return://接收到對方用戶信息 user_information_return(jsontext); break; case Agreement.user_trading_return://接收到用戶交易信息 user_trading_return(jsontext); break; case Agreement.user_trading_cancel_return://接收到用戶交易信息取消 user_trading_cancel_return(jsontext); break; case Agreement.user_trading_confirm_one_return://接收到用戶交易信息 確認1 user_trading_confirm_one_return(jsontext); break; case Agreement.user_trading_confirm_two_return://接收到用戶交易信息 確認2 user_trading_confirm_two_return(jsontext); break; case Agreement.user_trading_confirm_three_return://接收到用戶交易信息 確認3 user_trading_confirm_three_return(jsontext); break; case Agreement.user_trading_confirm_final_return://接收到用戶交易信息 最后確認 user_trading_confirm_final_return(jsontext); break; default: break; } } public virtual void user_offline_return(JsonData jsontext) { } public virtual void user_chat_return(JsonData jsontext) { } public virtual void user_battle_return(JsonData jsontext) { } public virtual void user_information_return(JsonData jsontext) { } public virtual void user_trading_return(JsonData jsontext) { } public virtual void user_trading_cancel_return(JsonData jsontext) { } public virtual void user_trading_confirm_one_return(JsonData jsontext) { } public virtual void user_trading_confirm_two_return(JsonData jsontext) { } public virtual void user_trading_confirm_three_return(JsonData jsontext) { } public virtual void user_trading_confirm_final_return(JsonData jsontext) { }
void Update() { if (JsonList.Count > 0) { JsonData mes = JsonList.Dequeue(); SplitDisposal(mes); } } //服務端返回后,數據賦值這里處理 void SplitDisposal(JsonData mes) { String Agreementstr = mes["Agreement"].ToString(); switch (Agreementstr) { case Agreement.user_chat_return: print(mes["username"] + "=====" + mes["message"] + "---" + mes["for"]); break; default: break; } } //根據需要 調用以下方法 public override void user_chat_return(JsonData jsontext) { base.user_chat_return(jsontext);//特殊字符提前處理 print(jsontext["username"] + "=====" + jsontext["message"] + "---" + jsontext["for"]); JsonList.Enqueue(jsontext); } public override void user_battle_return(JsonData jsontext) { } public override void user_information_return(JsonData jsontext) { }