|
@@ -10,7 +10,7 @@ using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using WebSocketSharp;
|
|
|
using WebSocketSharp.Server;
|
|
|
-using static InABox.Server.WebSocket.NotifyState;
|
|
|
+using static InABox.Server.WebSocket.PushState;
|
|
|
using InternalServer = WebSocketSharp.Server.WebSocketServer;
|
|
|
using Logger = InABox.Core.Logger;
|
|
|
|
|
@@ -18,7 +18,7 @@ namespace InABox.Server.WebSocket
|
|
|
{
|
|
|
public delegate void PollEvent(Session session);
|
|
|
|
|
|
- public class NotifyState
|
|
|
+ public class PushState
|
|
|
{
|
|
|
public class Session
|
|
|
{
|
|
@@ -46,13 +46,13 @@ namespace InABox.Server.WebSocket
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public class NotifyHandler : WebSocketBehavior
|
|
|
+ public class PushHandler : WebSocketBehavior
|
|
|
{
|
|
|
- private NotifyState NotifyState;
|
|
|
+ private PushState PushState;
|
|
|
|
|
|
- public NotifyHandler(NotifyState state)
|
|
|
+ public PushHandler(PushState state)
|
|
|
{
|
|
|
- NotifyState = state;
|
|
|
+ PushState = state;
|
|
|
}
|
|
|
|
|
|
protected override void OnOpen()
|
|
@@ -64,8 +64,8 @@ namespace InABox.Server.WebSocket
|
|
|
{
|
|
|
Logger.Send(LogType.Information, "", $"WebSocket client disconnected");
|
|
|
|
|
|
- var sessionID = NotifyState.SessionMap.Where(x => x.Value.ID == ID).FirstOrDefault().Key;
|
|
|
- NotifyState.SessionMap.TryRemove(sessionID, out var session);
|
|
|
+ var sessionID = PushState.SessionMap.Where(x => x.Value.ID == ID).FirstOrDefault().Key;
|
|
|
+ PushState.SessionMap.TryRemove(sessionID, out var session);
|
|
|
}
|
|
|
|
|
|
protected override void OnError(WebSocketSharp.ErrorEventArgs e)
|
|
@@ -76,8 +76,8 @@ namespace InABox.Server.WebSocket
|
|
|
private void DoInitial(InitialMessage initial)
|
|
|
{
|
|
|
var newSession = new Session(ID, initial.SessionID, initial.Platform);
|
|
|
- NotifyState.SessionMap[initial.SessionID] = newSession;
|
|
|
- NotifyState.Poll(newSession);
|
|
|
+ PushState.SessionMap[initial.SessionID] = newSession;
|
|
|
+ PushState.Poll(newSession);
|
|
|
}
|
|
|
|
|
|
protected override void OnMessage(MessageEventArgs e)
|
|
@@ -94,7 +94,7 @@ namespace InABox.Server.WebSocket
|
|
|
public class WebSocketServer
|
|
|
{
|
|
|
private InternalServer Server;
|
|
|
- private NotifyState NotifyState = new();
|
|
|
+ private PushState PushState = new();
|
|
|
|
|
|
public event PollEvent? Poll;
|
|
|
|
|
@@ -103,70 +103,70 @@ namespace InABox.Server.WebSocket
|
|
|
public WebSocketServer(int port)
|
|
|
{
|
|
|
Server = new InternalServer(IPAddress.Any, port);
|
|
|
- Server.AddWebSocketService("/notify", NewNotify);
|
|
|
+ Server.AddWebSocketService("/push", NewPush);
|
|
|
|
|
|
- NotifyState.OnPoll += NotifyState_Poll;
|
|
|
+ PushState.OnPoll += PushState_Poll;
|
|
|
}
|
|
|
|
|
|
- private void NotifyState_Poll(Session session)
|
|
|
+ private void PushState_Poll(Session session)
|
|
|
{
|
|
|
Poll?.Invoke(session);
|
|
|
}
|
|
|
|
|
|
- public NotifyHandler NewNotify()
|
|
|
+ public PushHandler NewPush()
|
|
|
{
|
|
|
- return new NotifyHandler(NotifyState);
|
|
|
+ return new PushHandler(PushState);
|
|
|
}
|
|
|
|
|
|
public IEnumerable<Guid> GetSessions(Platform platform)
|
|
|
{
|
|
|
- return NotifyState.SessionMap.Where(x => x.Value.Platform == platform).Select(x => x.Key);
|
|
|
+ return PushState.SessionMap.Where(x => x.Value.Platform == platform).Select(x => x.Key);
|
|
|
}
|
|
|
|
|
|
public void Push(Guid sessionID, SocketMessage message)
|
|
|
{
|
|
|
- if(NotifyState.SessionMap.TryGetValue(sessionID, out var session))
|
|
|
+ if(PushState.SessionMap.TryGetValue(sessionID, out var session))
|
|
|
{
|
|
|
using(var stream = new MemoryStream())
|
|
|
{
|
|
|
message.Write(stream);
|
|
|
- Server.WebSocketServices["/notify"].Sessions.SendToAsync(stream, (int)stream.Length, session.ID, (succ) => { });
|
|
|
+ Server.WebSocketServices["/push"].Sessions.SendToAsync(stream, (int)stream.Length, session.ID, (succ) => { });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void PushMessage(SocketMessage message)
|
|
|
{
|
|
|
- Server.WebSocketServices["/notify"].Sessions.Broadcast(message.WriteToBytes());
|
|
|
+ Server.WebSocketServices["/push"].Sessions.Broadcast(message.WriteToBytes());
|
|
|
}
|
|
|
private void PushMessage(SocketMessage message, string session)
|
|
|
{
|
|
|
- Server.WebSocketServices["/notify"].Sessions.SendTo(message.WriteToBytes(), session);
|
|
|
+ Server.WebSocketServices["/push"].Sessions.SendTo(message.WriteToBytes(), session);
|
|
|
}
|
|
|
|
|
|
- public void Push(Type TNotification, object notification)
|
|
|
+ public void Push(Type TPush, object push)
|
|
|
{
|
|
|
- PushMessage(NotifyMessage.Notify(TNotification, notification));
|
|
|
+ PushMessage(InABox.WebSocket.Shared.PushMessage.Push(TPush, push));
|
|
|
}
|
|
|
- public void Push<TNotification>(TNotification notification)
|
|
|
- where TNotification : BaseObject
|
|
|
+ public void Push<TPush>(TPush push)
|
|
|
+ where TPush : BaseObject
|
|
|
{
|
|
|
- PushMessage(NotifyMessage.Notify(notification));
|
|
|
+ PushMessage(InABox.WebSocket.Shared.PushMessage.Push(push));
|
|
|
}
|
|
|
|
|
|
- public void Push(Guid sessionID, Type TNotification, object notification)
|
|
|
+ public void Push(Guid sessionID, Type TPush, object push)
|
|
|
{
|
|
|
- if(NotifyState.SessionMap.TryGetValue(sessionID, out var session))
|
|
|
+ if(PushState.SessionMap.TryGetValue(sessionID, out var session))
|
|
|
{
|
|
|
- PushMessage(NotifyMessage.Notify(TNotification, notification), session.ID);
|
|
|
+ PushMessage(InABox.WebSocket.Shared.PushMessage.Push(TPush, push), session.ID);
|
|
|
}
|
|
|
}
|
|
|
- public void Push<TNotification>(Guid sessionID, TNotification notification)
|
|
|
- where TNotification : BaseObject
|
|
|
+ public void Push<TPush>(Guid sessionID, TPush push)
|
|
|
+ where TPush : BaseObject
|
|
|
{
|
|
|
- if(NotifyState.SessionMap.TryGetValue(sessionID, out var session))
|
|
|
+ if(PushState.SessionMap.TryGetValue(sessionID, out var session))
|
|
|
{
|
|
|
- PushMessage(NotifyMessage.Notify(notification), session.ID);
|
|
|
+ PushMessage(InABox.WebSocket.Shared.PushMessage.Push(push), session.ID);
|
|
|
}
|
|
|
}
|
|
|
|