diff --git a/zsirozas/Common.cs b/zsirozas/Common.cs index 0b8abb9bb6e441b02d3b4431bd2db7336f3172cc..d3fa3100da795a7f94a245e420339dc8507e5503 100644 --- a/zsirozas/Common.cs +++ b/zsirozas/Common.cs @@ -17,7 +17,7 @@ namespace zsirozas string CreateUser(string nickname, ConnectionType type, object backTalk); void RemoveUser(string userID); - string NewGame(string roomID); + string NewGame(string roomID);//TODO: hozzáadni hogy (string userID,...) string[] ListRooms(); string[] ListUsers(); ServerInfo ServerInfo(); diff --git a/zsirozas/SignalR-Transport.cs b/zsirozas/SignalR-Transport.cs new file mode 100644 index 0000000000000000000000000000000000000000..a97584d15950e8b4364d8ac633d17d814a88d34f --- /dev/null +++ b/zsirozas/SignalR-Transport.cs @@ -0,0 +1,130 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +//és a klienshez +using Microsoft.AspNet.SignalR.Client; +using Microsoft.AspNet.SignalR.Client.Transports; + +namespace zsirozas +{ + //............................................................ + class SignalRRemoteServer : IServerApiProvider + { + IHubProxy hub; + + public string CreateUser(string name, ConnectionType type, object backTalk) + { + return hub.Invoke<string>(nameof(CreateUser), name, type, backTalk).Result; + } + + public string CreateRoom(string userID) + { + return hub.Invoke<string>(nameof(CreateRoom), userID).Result; + } + + public void LeaveRoom(string userID) + { + hub.Invoke(nameof(LeaveRoom), userID).Wait(); + } + + public void JoinRoom(string userID, string roomID) + { + hub.Invoke(nameof(JoinRoom), roomID).Wait(); + } + + public void RemoveUser(string userID) + { + hub.Invoke(nameof(RemoveUser), userID).Wait(); + } + + public string NewGame(string roomID) + { + return hub.Invoke<string>(nameof(NewGame), roomID).Result; + } + + IAppClient client; + public SignalRRemoteServer(string server_url) + { + hub = new HubConnection(server_url).CreateHubProxy(nameof(SignalRServerHub)); + + hub.On<ServerEvent, object>(nameof(client.SimpleNotify), client.SimpleNotify); + hub.On<GameEvent, object>(nameof(client.InGameNotify), client.InGameNotify); + } + } + + //................................................................................ + + class SignalRServerHub : Hub<IAppClient>, IServerApiProvider + { + //a hubok tranziesnek, úgyhogy ezt csak így tudom értelmesen + //TODO: van-e ennek "szebb" módja? + public static IServerApiProvider appServer = new ActualServer(); + public SignalRServerHub(IServerApiProvider serverInstance) + { + appServer = serverInstance; + } + public string CreateUser(string name, ConnectionType type, object backTalk) + { + switch (type) + { + case ConnectionType.SignalR: + if (backTalk != null) goto default; + + backTalk = Context.ConnectionId; + break; + case ConnectionType.RawTCP: + throw new Exception("What? Why?"); + default: + throw new NotSupportedException("You can't do that"); + } + return appServer.CreateUser(name, type, backTalk); + } + + public string CreateRoom(string userID) + { + return appServer.CreateRoom(userID); + } + + public void LeaveRoom(string userID) + { + appServer.LeaveRoom(userID); + } + + public void JoinRoom(string userID, string roomID) + { + appServer.JoinRoom(userID, roomID); + } + + public void RemoveUser(string userID) + { + appServer.RemoveUser(userID); + } + + public string NewGame(string roomID) + { + return appServer.NewGame(roomID); + } + } + class AppClientWithSignalR : IAppClient + { + public string signalRConnID; + + public AppClientWithSignalR(string backTalk) + { + this.signalRConnID = backTalk; + } + + public void InGameNotify(GameEvent @event, object param0) + { + var context = GlobalHost.ConnectionManager.GetHubContext<SignalRServerHub>(); + (context as IAppClient).InGameNotify(@event, param0); + } + + public void SimpleNotify(ServerEvent @event, object param0) + { + var context = GlobalHost.ConnectionManager.GetHubContext<SignalRServerHub>(); + (context as IAppClient).SimpleNotify(@event, param0); + } + } + +} \ No newline at end of file diff --git a/zsirozas/zsirozas.csproj b/zsirozas/zsirozas.csproj index 57cd48cd3944db385e4da232fb517bd8f8f657b1..763a1beb14c02a9540b3d1753b22e868686b56a4 100644 --- a/zsirozas/zsirozas.csproj +++ b/zsirozas/zsirozas.csproj @@ -61,6 +61,7 @@ <Compile Include="Common.cs" /> <Compile Include="RawTcpTransport.cs" /> <Compile Include="Rooms.cs" /> + <Compile Include="SignalR-Transport.cs" /> </ItemGroup> <ItemGroup> <None Include="app.config" />