diff --git a/README.md b/README.md index 71a9b73..f7b14df 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Create and initialize a new pomelo client. ```c# PomeloClient pclient = new PomeloClient(url); -pclient.init(); +pclient.init( Action onConnect , Action onDisconnect , Action onError ); ``` diff --git a/SimpleJson.dll b/SimpleJson.dll new file mode 100644 index 0000000..bc5d496 Binary files /dev/null and b/SimpleJson.dll differ diff --git a/SocketIO.dll b/SocketIO.dll new file mode 100755 index 0000000..15582d6 Binary files /dev/null and b/SocketIO.dll differ diff --git a/SuperSocket.ClientEngine.Common.dll b/SuperSocket.ClientEngine.Common.dll new file mode 100644 index 0000000..da80fa4 Binary files /dev/null and b/SuperSocket.ClientEngine.Common.dll differ diff --git a/SuperSocket.ClientEngine.Core.dll b/SuperSocket.ClientEngine.Core.dll new file mode 100644 index 0000000..de8e32c Binary files /dev/null and b/SuperSocket.ClientEngine.Core.dll differ diff --git a/SuperSocket.ClientEngine.Protocol.dll b/SuperSocket.ClientEngine.Protocol.dll new file mode 100644 index 0000000..3fc476c Binary files /dev/null and b/SuperSocket.ClientEngine.Protocol.dll differ diff --git a/SuperSocket.ClientEngine.Proxy.dll b/SuperSocket.ClientEngine.Proxy.dll new file mode 100644 index 0000000..a23906d Binary files /dev/null and b/SuperSocket.ClientEngine.Proxy.dll differ diff --git a/WebSocket4Net.dll b/WebSocket4Net.dll new file mode 100644 index 0000000..013235e Binary files /dev/null and b/WebSocket4Net.dll differ diff --git a/pomelo-unityclient.userprefs b/pomelo-unityclient.userprefs index d69a729..dbb45b6 100644 --- a/pomelo-unityclient.userprefs +++ b/pomelo-unityclient.userprefs @@ -1,9 +1,35 @@  - - + + - + + + + + + + + + + + + + + + + + + + + + bytes[0] + bytes[1] + m + n + + + diff --git a/pomelo-unityclient/bin/Debug/SocketIO.dll b/pomelo-unityclient/bin/Debug/SocketIO.dll old mode 100644 new mode 100755 index 9988897..15582d6 Binary files a/pomelo-unityclient/bin/Debug/SocketIO.dll and b/pomelo-unityclient/bin/Debug/SocketIO.dll differ diff --git a/pomelo-unityclient/bin/Debug/pomelo-unityclient.dll b/pomelo-unityclient/bin/Debug/pomelo-unityclient.dll old mode 100644 new mode 100755 index 091958a..3bf366c Binary files a/pomelo-unityclient/bin/Debug/pomelo-unityclient.dll and b/pomelo-unityclient/bin/Debug/pomelo-unityclient.dll differ diff --git a/pomelo-unityclient/bin/Debug/pomelo-unityclient.dll.mdb b/pomelo-unityclient/bin/Debug/pomelo-unityclient.dll.mdb new file mode 100644 index 0000000..d6e8f57 Binary files /dev/null and b/pomelo-unityclient/bin/Debug/pomelo-unityclient.dll.mdb differ diff --git a/pomelo-unityclient/bin/Release/SimpleJson.dll b/pomelo-unityclient/bin/Release/SimpleJson.dll new file mode 100644 index 0000000..bc5d496 Binary files /dev/null and b/pomelo-unityclient/bin/Release/SimpleJson.dll differ diff --git a/pomelo-unityclient/bin/Release/SocketIO.dll b/pomelo-unityclient/bin/Release/SocketIO.dll new file mode 100755 index 0000000..15582d6 Binary files /dev/null and b/pomelo-unityclient/bin/Release/SocketIO.dll differ diff --git a/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Common.dll b/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Common.dll new file mode 100644 index 0000000..da80fa4 Binary files /dev/null and b/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Common.dll differ diff --git a/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Core.dll b/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Core.dll new file mode 100644 index 0000000..de8e32c Binary files /dev/null and b/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Core.dll differ diff --git a/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Protocol.dll b/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Protocol.dll new file mode 100644 index 0000000..3fc476c Binary files /dev/null and b/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Protocol.dll differ diff --git a/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Proxy.dll b/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Proxy.dll new file mode 100644 index 0000000..a23906d Binary files /dev/null and b/pomelo-unityclient/bin/Release/SuperSocket.ClientEngine.Proxy.dll differ diff --git a/pomelo-unityclient/bin/Release/UnityEngine.dll b/pomelo-unityclient/bin/Release/UnityEngine.dll new file mode 100755 index 0000000..745ad13 Binary files /dev/null and b/pomelo-unityclient/bin/Release/UnityEngine.dll differ diff --git a/pomelo-unityclient/bin/Release/WebSocket4Net.dll b/pomelo-unityclient/bin/Release/WebSocket4Net.dll new file mode 100644 index 0000000..013235e Binary files /dev/null and b/pomelo-unityclient/bin/Release/WebSocket4Net.dll differ diff --git a/pomelo-unityclient/bin/Release/pomelo-unityclient.dll b/pomelo-unityclient/bin/Release/pomelo-unityclient.dll new file mode 100755 index 0000000..5cb3b3c Binary files /dev/null and b/pomelo-unityclient/bin/Release/pomelo-unityclient.dll differ diff --git a/pomelo-unityclient/pomelo-unityclient.csproj b/pomelo-unityclient/pomelo-unityclient.csproj index bd9e02f..a2eb5d9 100644 --- a/pomelo-unityclient/pomelo-unityclient.csproj +++ b/pomelo-unityclient/pomelo-unityclient.csproj @@ -12,52 +12,56 @@ v3.5 - true + True full - false + False bin\Debug DEBUG; prompt 4 x86 - false + False none - false + False bin\Release prompt 4 x86 - false + False + - ..\..\..\Chat\Assets\Plugins\SimpleJson.dll + ..\SimpleJson.dll - ..\..\..\Chat\Assets\Plugins\SocketIO.dll + ..\SocketIO.dll - ..\..\..\Chat\Assets\Plugins\SuperSocket.ClientEngine.Common.dll + ..\SuperSocket.ClientEngine.Common.dll - ..\..\..\Chat\Assets\Plugins\SuperSocket.ClientEngine.Core.dll + ..\SuperSocket.ClientEngine.Core.dll - ..\..\..\Chat\Assets\Plugins\SuperSocket.ClientEngine.Protocol.dll + ..\SuperSocket.ClientEngine.Protocol.dll - ..\..\..\Chat\Assets\Plugins\SuperSocket.ClientEngine.Proxy.dll + ..\SuperSocket.ClientEngine.Proxy.dll - ..\..\..\Chat\Assets\Plugins\WebSocket4Net.dll + ..\WebSocket4Net.dll + + + ..\..\..\..\..\..\..\Applications\Unity\Unity.app\Contents\Frameworks\Managed\UnityEngine.dll \ No newline at end of file diff --git a/pomelo-unityclient/pomelo-unityclient/ClientUpdater.cs b/pomelo-unityclient/pomelo-unityclient/ClientUpdater.cs new file mode 100644 index 0000000..cdc7fc7 --- /dev/null +++ b/pomelo-unityclient/pomelo-unityclient/ClientUpdater.cs @@ -0,0 +1,112 @@ +using UnityEngine; +using System; +using System.Collections.Generic; +using SimpleJson; + +// TranspotUpdate.cs +// Author: Lu Zexi +// 2014-6-20 + + + +namespace pomeloUnityClient +{ + /// + /// Transpot updater. + /// + public class ClientUpdater : MonoBehaviour + { + private enum STATE + { + NONE = 0, + START = 1, + SOCKET_ERROR = 2, + RUNING = 3, + CLOSE = 4, + } + private STATE m_eStat = STATE.NONE; //the state of the transpotUpdate + private Action m_cUpdate; //update action + private Action m_cOnOpen; //open event + private Action m_cOnClose; //close event + private Action m_cOnSocketError; //socket event + + /// + /// Init this instance. + /// + internal static ClientUpdater Init( Action onConnect , Action onDisconnect , Action socketError , Action onUpdate ) + { + GameObject obj = new GameObject("Socket"); + ClientUpdater trans = obj.AddComponent(); + trans.m_cOnOpen = onConnect; + trans.m_cOnClose = onDisconnect; + trans.m_cOnSocketError = socketError; + trans.m_cUpdate = onUpdate; + return trans; + } + + /// + /// close the updater + /// + internal void _Close() + { + this.m_eStat = STATE.CLOSE; + } + + /// + /// _s the error. + /// + internal void _Error() + { + this.m_eStat = STATE.SOCKET_ERROR; + } + + /// + /// Start this update. + /// + internal void _Start() + { + this.m_eStat = STATE.START; + } + + /// + /// Fixeds the update. + /// + void FixedUpdate() + { + switch(this.m_eStat) + { + case STATE.START: + if(this.m_cOnOpen != null ) + { + this.m_cOnOpen(); + } + this.m_cOnOpen = null; + this.m_eStat = STATE.RUNING; + break; + case STATE.SOCKET_ERROR: + if(this.m_cOnSocketError != null ) + { + this.m_cOnSocketError(); + } + this.m_eStat = STATE.RUNING; + break; + case STATE.RUNING: + if(this.m_cUpdate != null ) + { + this.m_cUpdate(); + } + break; + case STATE.CLOSE: + if(this.m_cOnClose != null ) + { + this.m_cOnClose(); + } + this.m_cOnClose = null; + this.m_eStat = STATE.NONE; + GameObject.Destroy(gameObject); + break; + } + } + } +} + diff --git a/pomelo-unityclient/pomelo-unityclient/EventManager.cs b/pomelo-unityclient/pomelo-unityclient/EventManager.cs index 76edf5d..950ce31 100644 --- a/pomelo-unityclient/pomelo-unityclient/EventManager.cs +++ b/pomelo-unityclient/pomelo-unityclient/EventManager.cs @@ -1,3 +1,4 @@ +#define LUZEXI using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -84,7 +85,24 @@ public void InvokeOnEvent (JsonObject msg) { } } } - + +#if LUZEXI + /// + /// Gets the event. + /// + /// The event. + /// Event name. + public List> GetEvent( string eventName ) + { + List> lst = new List>(); + + if( this.eventMap.ContainsKey(eventName)) + { + lst = eventMap[eventName]; + } + return lst; + } +#endif // Dispose() calls Dispose(true) public void Dispose() diff --git a/pomelo-unityclient/pomelo-unityclient/PomeloClient.cs b/pomelo-unityclient/pomelo-unityclient/PomeloClient.cs index cc38773..ffe5c2a 100644 --- a/pomelo-unityclient/pomelo-unityclient/PomeloClient.cs +++ b/pomelo-unityclient/pomelo-unityclient/PomeloClient.cs @@ -1,8 +1,11 @@ +#define LUZEXI using System; using System.Text; using System.Diagnostics; using SimpleJson; using System.Collections.Generic; +using System.Collections; +using System.Collections.Concurrent; using SocketIOClient; namespace pomeloUnityClient @@ -14,7 +17,13 @@ public class PomeloClient private const string ARRAY_FLAG = "["; private const string URL_HEADER = "http://"; private EventManager eventManager = null; - + +#if LUZEXI + private ClientUpdater m_cUpdater = null; //client updater + private const int PROCESS_NUM = 5; //the process message num per fps + private ConcurrentQueue m_seqMsg = new ConcurrentQueue(); //the seq of the message. +#endif + public PomeloClient (string url) { string checkedUrl = this.checkUrl(url); @@ -29,8 +38,10 @@ public PomeloClient (string url) } //Init socket and make connection. - public void init(){ - + public void init( Action onConnect , Action onDisconnect , Action onError ){ +#if LUZEXI + this.m_cUpdater = ClientUpdater.Init( onConnect , onDisconnect ,onError, Update ); +#endif this.socket.Opened += this.SocketOpened; this.socket.Message += this.SocketMessage; this.socket.SocketConnectionClosed += this.SocketConnectionClosed; @@ -60,7 +71,6 @@ public void disconnect(){ this.eventManager = null; } this.closeSocketIO(); - } /// @@ -137,6 +147,9 @@ private JsonObject filter(JsonObject msg) { /// private void processMessage(string msg){ JsonObject jsonMsg = (JsonObject)SimpleJson.SimpleJson.DeserializeObject(msg); +#if LUZEXI + this.m_seqMsg.Enqueue(jsonMsg); +#else Object id = null; //-----------the request and notify message from server----------------- if (jsonMsg.TryGetValue("id", out id)) { @@ -145,7 +158,29 @@ private void processMessage(string msg){ } else { this.eventManager.InvokeOnEvent(jsonMsg); } +#endif + } + +#if LUZEXI + internal void Update() + { + for( int i = 0 ; i @@ -195,11 +233,18 @@ private void SocketMessage (object sender, MessageEventArgs e) { //Connetction close event. private void SocketConnectionClosed (object sender, EventArgs e) { +#if LUZEXI + this.m_cUpdater._Close(); + this.m_cUpdater = null; +#endif Console.WriteLine("WebSocketConnection was terminated!"); } //Connection error event. private void SocketError (object sender, ErrorEventArgs e) { +#if LUZEXI + this.m_cUpdater._Error(); +#endif Console.WriteLine("socket client error:"); Console.WriteLine(e.Message); }