The code to learn SuperSocket must be noted is
Static voidMain (string[] args) {Websocketserver AppServer=NewWebsocketserver (); if(!appserver.setup ( -))//Port{Console.WriteLine ("Port Error!"); Console.readkey (); return; } appserver.newsessionconnected+=NewSessionhandler<websocketsession>(appserver_newclientconnected); Appserver.newmessagereceived+=NewSessionhandler<websocketsession,string>(appserver_newmessagereceived); Appserver.sessionclosed+=NewSessionhandler<websocketsession, closereason>(appserver_sessionclosed); if(!Appserver.start ()) {Console.WriteLine ("Startup Error"); Console.readkey (); return; } Console.WriteLine ("server started successfully, press ' Q ' to exit the server!"); while(Console.readkey (). KeyChar! ='Q') {Console.WriteLine (); Continue; } appserver.stop (); Console.WriteLine (); Console.WriteLine ("Server shutdown"); Console.readkey (); }
The code that must be noted for learning unity communication is
// server IP address and port number serveradress = " ws://192.168.1.104:2000/ " = new WebSocket (serveradress) ; WebSocket. Open ();
Public void Addlistenersocket () { new EventHandler (websocket_opened); New Eventhandler<erroreventargs>(websocket_error); New EventHandler (websocket_closed); New Eventhandler<messagereceivedeventargs>(websocket_messagereceived);
//Login Send Listener Private voidWebsocket_opened (Objectsender, EventArgs e) {Jsondata message=NewJsondata (); message["Agreement"] =Agreement.user_no_return; message["username"] =_socketmodel.playname; _socketmodel.websocket.send (message. ToJson ());//This is sent to the message end . } //Turn off monitoring Public Virtual voidWebsocket_closed (Objectsender, EventArgs e) { } //Receiving monitoring Public voidWebsocket_messagereceived (Objectsender, Messagereceivedeventargs e) {Jsondata Jsontext= Jsonmapper.toobject (e.message);//Convert data into JSON format stringagreements = jsontext["Agreement"]. ToString (); Switch(agreements) { CaseAgreement.user_offline_return://receive the user offline notificationUser_offline_return (Jsontext); Break; CaseAgreement.user_chat_return://Receive chat messagesUser_chat_return (Jsontext); Break; CaseAgreement.user_battle_return://Receive PVP notificationUser_battle_return (Jsontext); Break; CaseAgreement.user_information_return://receive information about the other userUser_information_return (Jsontext); Break; CaseAgreement.user_trading_return://Receive user transaction informationUser_trading_return (Jsontext); Break; CaseAgreement.user_trading_cancel_return://receive user transaction information cancellationUser_trading_cancel_return (Jsontext); Break; CaseAgreement.user_trading_confirm_one_return://received the user transaction information confirmation 1User_trading_confirm_one_return (Jsontext); Break; CaseAgreement.user_trading_confirm_two_return://received the user transaction information Confirmation 2User_trading_confirm_two_return (Jsontext); Break; CaseAgreement.user_trading_confirm_three_return://received the user transaction information Confirmation 3User_trading_confirm_three_return (Jsontext); Break; CaseAgreement.user_trading_confirm_final_return://Receive user transaction information final confirmationUser_trading_confirm_final_return (Jsontext); Break; default: Break; } } Public Virtual voidUser_offline_return (Jsondata jsontext) {} Public Virtual voidUser_chat_return (Jsondata jsontext) {} Public Virtual voidUser_battle_return (Jsondata jsontext) {} Public Virtual voidUser_information_return (Jsondata jsontext) {} Public Virtual voidUser_trading_return (Jsondata jsontext) {} Public Virtual voidUser_trading_cancel_return (Jsondata jsontext) {} Public Virtual voidUser_trading_confirm_one_return (Jsondata jsontext) {} Public Virtual voidUser_trading_confirm_two_return (Jsondata jsontext) {} Public Virtual voidUser_trading_confirm_three_return (Jsondata jsontext) {} Public Virtual voidUser_trading_confirm_final_return (Jsondata jsontext) {}
voidUpdate () {if(Jsonlist.count >0) {jsondata mes=Jsonlist.dequeue (); Splitdisposal (MES); } } //after the server returns, the data assignment is processed here voidsplitdisposal (jsondata mes) {String agreementstr= mes["Agreement"]. ToString (); Switch(agreementstr) { CaseAgreement.user_chat_return:print (mes["username"] +"====="+ mes["message"] +"---"+ mes[" for"]); Break; default: Break; } } //call the following methods as needed Public Override voidUser_chat_return (Jsondata jsontext) {Base. User_chat_return (Jsontext);//special characters Advance processingPrint (jsontext["username"] +"====="+ jsontext["message"] +"---"+ jsontext[" for"]); Jsonlist.enqueue (Jsontext); } Public Override voidUser_battle_return (Jsondata jsontext) {} Public Override voidUser_information_return (Jsondata jsontext) {}
Supersocket+unity Network Notes