Skip to content
Open

ok #3

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

```

Expand Down
Binary file added SimpleJson.dll
Binary file not shown.
Binary file added SocketIO.dll
Binary file not shown.
Binary file added SuperSocket.ClientEngine.Common.dll
Binary file not shown.
Binary file added SuperSocket.ClientEngine.Core.dll
Binary file not shown.
Binary file added SuperSocket.ClientEngine.Protocol.dll
Binary file not shown.
Binary file added SuperSocket.ClientEngine.Proxy.dll
Binary file not shown.
Binary file added WebSocket4Net.dll
Binary file not shown.
32 changes: 29 additions & 3 deletions pomelo-unityclient.userprefs
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" />
<MonoDevelop.Ide.Workbench ActiveDocument="pomelo-unityclient\pomelo-unityclient\PomeloClient.cs">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release|x86" />
<MonoDevelop.Ide.Workbench ActiveDocument="pomelo-unityclient/pomelo-unityclient/PomeloClient.cs">
<Files>
<File FileName="pomelo-unityclient\pomelo-unityclient\PomeloClient.cs" Line="167" Column="5" />
<File FileName="pomelo-unityclient/pomelo-unityclient/PomeloClient.cs" Line="7" Column="25" />
<File FileName="pomelo-unityclient/pomelo-unityclient/EventManager.cs" Line="1" Column="1" />
<File FileName="pomelo-unityclient/pomelo-unityclient/Protocol.cs" Line="1" Column="1" />
<File FileName="pomelo-unityclient/pomelo-unityclient/ClientUpdater.cs" Line="59" Column="4" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State expanded="True">
<Node name="pomelo-unityclient" expanded="True">
<Node name="References" expanded="True" />
<Node name="pomelo-unityclient" expanded="True">
<Node name="PomeloClient.cs" selected="True" />
</Node>
</Node>
</State>
</Pad>
<Pad Id="ClassPad">
<State expanded="True" selected="True" />
</Pad>
<Pad Id="MonoDevelop.Debugger.WatchPad">
<State>
<Value>bytes[0]</Value>
<Value>bytes[1]</Value>
<Value>m</Value>
<Value>n</Value>
</State>
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
Expand Down
Binary file modified pomelo-unityclient/bin/Debug/SocketIO.dll
100644 → 100755
Binary file not shown.
Binary file modified pomelo-unityclient/bin/Debug/pomelo-unityclient.dll
100644 → 100755
Binary file not shown.
Binary file not shown.
Binary file added pomelo-unityclient/bin/Release/SimpleJson.dll
Binary file not shown.
Binary file added pomelo-unityclient/bin/Release/SocketIO.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added pomelo-unityclient/bin/Release/UnityEngine.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
28 changes: 16 additions & 12 deletions pomelo-unityclient/pomelo-unityclient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,56 @@
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<ConsolePause>false</ConsolePause>
<ConsolePause>False</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<Optimize>False</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<ConsolePause>false</ConsolePause>
<ConsolePause>False</ConsolePause>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Compile Include="pomelo-unityclient\EventManager.cs" />
<Compile Include="pomelo-unityclient\PomeloClient.cs" />
<Compile Include="pomelo-unityclient\Protocol.cs" />
<Compile Include="pomelo-unityclient\ClientUpdater.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="SimpleJson">
<HintPath>..\..\..\Chat\Assets\Plugins\SimpleJson.dll</HintPath>
<HintPath>..\SimpleJson.dll</HintPath>
</Reference>
<Reference Include="SocketIO">
<HintPath>..\..\..\Chat\Assets\Plugins\SocketIO.dll</HintPath>
<HintPath>..\SocketIO.dll</HintPath>
</Reference>
<Reference Include="SuperSocket.ClientEngine.Common">
<HintPath>..\..\..\Chat\Assets\Plugins\SuperSocket.ClientEngine.Common.dll</HintPath>
<HintPath>..\SuperSocket.ClientEngine.Common.dll</HintPath>
</Reference>
<Reference Include="SuperSocket.ClientEngine.Core">
<HintPath>..\..\..\Chat\Assets\Plugins\SuperSocket.ClientEngine.Core.dll</HintPath>
<HintPath>..\SuperSocket.ClientEngine.Core.dll</HintPath>
</Reference>
<Reference Include="SuperSocket.ClientEngine.Protocol">
<HintPath>..\..\..\Chat\Assets\Plugins\SuperSocket.ClientEngine.Protocol.dll</HintPath>
<HintPath>..\SuperSocket.ClientEngine.Protocol.dll</HintPath>
</Reference>
<Reference Include="SuperSocket.ClientEngine.Proxy">
<HintPath>..\..\..\Chat\Assets\Plugins\SuperSocket.ClientEngine.Proxy.dll</HintPath>
<HintPath>..\SuperSocket.ClientEngine.Proxy.dll</HintPath>
</Reference>
<Reference Include="WebSocket4Net">
<HintPath>..\..\..\Chat\Assets\Plugins\WebSocket4Net.dll</HintPath>
<HintPath>..\WebSocket4Net.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\..\..\..\..\..\..\Applications\Unity\Unity.app\Contents\Frameworks\Managed\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
112 changes: 112 additions & 0 deletions pomelo-unityclient/pomelo-unityclient/ClientUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using UnityEngine;
using System;
using System.Collections.Generic;
using SimpleJson;

// TranspotUpdate.cs
// Author: Lu Zexi
// 2014-6-20



namespace pomeloUnityClient
{
/// <summary>
/// Transpot updater.
/// </summary>
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

/// <summary>
/// Init this instance.
/// </summary>
internal static ClientUpdater Init( Action onConnect , Action onDisconnect , Action socketError , Action onUpdate )
{
GameObject obj = new GameObject("Socket");
ClientUpdater trans = obj.AddComponent<ClientUpdater>();
trans.m_cOnOpen = onConnect;
trans.m_cOnClose = onDisconnect;
trans.m_cOnSocketError = socketError;
trans.m_cUpdate = onUpdate;
return trans;
}

/// <summary>
/// close the updater
/// </summary>
internal void _Close()
{
this.m_eStat = STATE.CLOSE;
}

/// <summary>
/// _s the error.
/// </summary>
internal void _Error()
{
this.m_eStat = STATE.SOCKET_ERROR;
}

/// <summary>
/// Start this update.
/// </summary>
internal void _Start()
{
this.m_eStat = STATE.START;
}

/// <summary>
/// Fixeds the update.
/// </summary>
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;
}
}
}
}

20 changes: 19 additions & 1 deletion pomelo-unityclient/pomelo-unityclient/EventManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define LUZEXI
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -84,7 +85,24 @@ public void InvokeOnEvent (JsonObject msg) {
}
}
}


#if LUZEXI
/// <summary>
/// Gets the event.
/// </summary>
/// <returns>The event.</returns>
/// <param name="eventName">Event name.</param>
public List<Action<JsonObject>> GetEvent( string eventName )
{
List<Action<JsonObject>> lst = new List<Action<JsonObject>>();

if( this.eventMap.ContainsKey(eventName))
{
lst = eventMap[eventName];
}
return lst;
}
#endif

// Dispose() calls Dispose(true)
public void Dispose()
Expand Down
53 changes: 49 additions & 4 deletions pomelo-unityclient/pomelo-unityclient/PomeloClient.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<JsonObject> m_seqMsg = new ConcurrentQueue<JsonObject>(); //the seq of the message.
#endif

public PomeloClient (string url)
{
string checkedUrl = this.checkUrl(url);
Expand All @@ -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;
Expand Down Expand Up @@ -60,7 +71,6 @@ public void disconnect(){
this.eventManager = null;
}
this.closeSocketIO();

}

/// <summary>
Expand Down Expand Up @@ -137,6 +147,9 @@ private JsonObject filter(JsonObject msg) {
/// </param>
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)) {
Expand All @@ -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<PROCESS_NUM ; i++ )
{
JsonObject jsonMsg;
if( this.m_seqMsg.TryDequeue(out jsonMsg) )
{
Object id = null;
//-----------the request and notify message from server-----------------
if (jsonMsg.TryGetValue("id", out id)) {
this.eventManager.InvokeCallBack(jsonMsg);
//------------bordcast message form server------------------------------
} else {
this.eventManager.InvokeOnEvent(jsonMsg);
}
}
}
}
#endif

//Processes the message and invoke callback or event.
private void processMessageBatch(string msgs){
Expand All @@ -170,6 +205,9 @@ private void closeSocketIO(){

//connection opened event.
private void SocketOpened (object sender, EventArgs e){
#if LUZEXI
this.m_cUpdater._Start();
#endif
Console.WriteLine("The socketIO opend!");
}
/// <summary>
Expand All @@ -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);
}
Expand Down