Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Add namespace feature SocketIOConnection. #19

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ private void CallHandler(SocketIOPacket Packet)
{
JToken Event = Temp.Dequeue();
JToken[] Data = Temp.ToArray();

CallEventHandler(Event, Data);
CallAckEventHandler(Event, Packet.ID, Data);
Temp.Enqueue(Packet.Namespace);
CallEventHandler(Event, Temp.ToArray());
CallAckEventHandler(Event, Packet.ID, Packet.Namespace, Data);
}
}
}

private void CallAckEventHandler(JToken Event, int PacketID, params JToken[] Data)
private void CallAckEventHandler(JToken Event, int PacketID, string ns, params JToken[] Data)
{
if (Event != null)
{
Expand All @@ -105,7 +105,7 @@ private void CallAckEventHandler(JToken Event, int PacketID, params JToken[] Dat
{
Callback = (AckData) =>
{
Emit(SocketIOPacket.CreateAckPacket(new JArray(AckData), PacketID));
Emit(SocketIOPacket.CreateAckPacket(new JArray(AckData), PacketID, ns));
};
}

Expand Down
10 changes: 6 additions & 4 deletions SocketIOSharp/Common/Abstract/Connection/SocketIOConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void OnPacket(SocketIOPacket Packet)
switch (Packet.Type)
{
case SocketIOPacketType.CONNECT:
OnConnect();
OnConnect(Packet);
break;

case SocketIOPacketType.DISCONNECT:
Expand Down Expand Up @@ -61,9 +61,11 @@ private void OnPacket(SocketIOPacket Packet)
}
}

private void OnConnect()
private void OnConnect(SocketIOPacket Packet)
{
CallEventHandler(SocketIOEvent.CONNECTION);
CallEventHandler(SocketIOEvent.CONNECTION, Packet.Namespace);

Emit(SocketIOPacket.CreateConnectionPacket(Packet.Namespace));
}

protected void OnDisconnect(Exception Exception)
Expand All @@ -89,7 +91,7 @@ private void OnAck(SocketIOPacket Packet)

private void OnError(SocketIOPacket Packet)
{
CallEventHandler(SocketIOEvent.ERROR, Packet?.JsonData?.ToString() ?? string.Empty);
CallEventHandler(SocketIOEvent.ERROR, Packet.Namespace, Packet?.JsonData?.ToString() ?? string.Empty);
}

private void OnBinaryEvent(SocketIOPacket Packet)
Expand Down
12 changes: 6 additions & 6 deletions SocketIOSharp/Common/Abstract/SocketIO.Emit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ private object[] Arguments(params object[] Arguments)
return Result;
}

public TChildClass Emit(JToken Event, Action<JToken[]> Callback = null)
public TChildClass Emit(JToken Event, string ns = "/", Action<JToken[]> Callback = null)
{
return Emit(Event, Arguments(Callback));
return Emit(Event, ns, Arguments(Callback));
}

public TChildClass Emit(JToken Event, JToken Data, Action<JToken[]> Callback = null)
public TChildClass Emit(JToken Event, JToken Data, string ns = "/", Action<JToken[]> Callback = null)
{
return Emit(Event, Arguments(Data, Callback));
return Emit(Event, ns, Arguments(Data, Callback));
}

public TChildClass Emit(JToken Event, params object[] Arguments)
private TChildClass Emit(JToken Event, string ns, params object[] Arguments)
{
if (Event != null)
{
Expand All @@ -61,7 +61,7 @@ public TChildClass Emit(JToken Event, params object[] Arguments)
JsonArray.Add(Data);
}

Emit(SocketIOPacket.CreateEventPacket(JsonArray, AckManager.CreateAck(Callback)));
Emit(SocketIOPacket.CreateEventPacket(JsonArray, AckManager.CreateAck(Callback), ns));
}

return this as TChildClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,31 @@ internal static SocketIOPacket Decode(string Data)
Packet.Namespace = "/";
}

char Next = Data[Offset + 1];

if (!char.IsWhiteSpace(Next) && char.IsNumber(Next))
if (Offset < Data.Length - 1)
{
StringBuilder Builder = new StringBuilder();
char Next = Data[Offset + 1];

while (Offset < Data.Length - 1)
if (!char.IsWhiteSpace(Next) && char.IsNumber(Next))
{
char c = Data[++Offset];
StringBuilder Builder = new StringBuilder();

if (char.IsNumber(c))
{
Builder.Append(c);
}
else
while (Offset < Data.Length - 1)
{
--Offset;
break;
char c = Data[++Offset];

if (char.IsNumber(c))
{
Builder.Append(c);
}
else
{
--Offset;
break;
}
}
}

Packet.ID = int.Parse(Builder.ToString());
Packet.ID = int.Parse(Builder.ToString());
}
}

if (++Offset < Data.Length - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ namespace SocketIOSharp.Common.Packet
{
partial class SocketIOPacket
{
internal static SocketIOPacket CreateConnectionPacket()
internal static SocketIOPacket CreateConnectionPacket(string ns = "/")
{
return new SocketIOPacket()
{
Type = SocketIOPacketType.CONNECT,
IsJson = true
IsJson = true,
Namespace = ns
};
}

internal static SocketIOPacket CreateEventPacket(JArray JsonArray, SocketIOAck Ack)
internal static SocketIOPacket CreateEventPacket(JArray JsonArray, SocketIOAck Ack, string ns)
{
SocketIOPacket Packet = CreateMessagePacket(JsonArray, Ack);
SocketIOPacket Packet = CreateMessagePacket(JsonArray, Ack, ns);

if (Packet.IsJson)
{
Expand All @@ -31,11 +32,11 @@ internal static SocketIOPacket CreateEventPacket(JArray JsonArray, SocketIOAck A
return Packet;
}

internal static SocketIOPacket CreateAckPacket(JArray JsonArray, int PacketID)
internal static SocketIOPacket CreateAckPacket(JArray JsonArray, int PacketID, string ns = "/")
{
if (PacketID >= 0)
{
SocketIOPacket Packet = CreateMessagePacket(JsonArray, PacketID);
SocketIOPacket Packet = CreateMessagePacket(JsonArray, PacketID, ns);

if (Packet.IsJson)
{
Expand All @@ -55,17 +56,18 @@ internal static SocketIOPacket CreateAckPacket(JArray JsonArray, int PacketID)
}
}

private static SocketIOPacket CreateMessagePacket(JArray JsonArray, SocketIOAck Ack)
private static SocketIOPacket CreateMessagePacket(JArray JsonArray, SocketIOAck Ack, string ns)
{
return CreateMessagePacket(JsonArray, Ack == null ? -1 : Ack.PacketID);
return CreateMessagePacket(JsonArray, Ack == null ? -1 : Ack.PacketID, ns);
}

private static SocketIOPacket CreateMessagePacket(JArray JsonArray, int PacketID)
private static SocketIOPacket CreateMessagePacket(JArray JsonArray, int PacketID, string ns)
{
SocketIOPacket Packet = new SocketIOPacket
{
JsonData = JsonArray,
IsJson = true
IsJson = true,
Namespace = ns
};

if (PacketID >= 0)
Expand All @@ -87,4 +89,4 @@ private static SocketIOPacket CreateMessagePacket(JArray JsonArray, int PacketID
return Packet;
}
}
}
}