API / RPC / webSocket three looks nothing like the same place, at the time of development, services, client implementation code is also quite different
Recently sorted out, through dynamic proxy form, the integration of these developments, all through a unified interface constraints, server implementation and client calls
Based on this form, WebAPI / RPC / webSocket only you need to define a set of interfaces, can achieve the effect of GM
Example interface constraint
public class testObj { public string Name { get; set; } } public interface ITestService { void Login(); bool Test1(int a,int? b,out string error); TestObj Test2 (TestObj obj); } public class TestService : AbsService, ITestService { [LoginPoint] public void Login() { SaveSession("hubro", "7777777777", "test"); } public bool Test1(int a, int? b, out string error) { var user = CurrentUserName; var tag = CurrentUserTag; error = "out error"; Console.WriteLine(a); Console.WriteLine(b); return true; } public TestObj Test2(TestObj obj) { Console.WriteLine(obj.ToJson()); return obj; } }
The above is a standard interface and the interface, and inherits AbsService,
Login methods marked LoginPoint characteristics, represents a login entry point, calling this method must first call this interface, the login status within the storage method SaveSession
Use the CurrentUserName within Test1 method to obtain the login method saved Session
Features:
- By interface constraints server and client calls, parameters defined transparency, out also support
- The real remote method invocation, any parameter type, number
- Integrated login authentication logic, customize the login authentication process, you can also customize Session achieve
- Simple integration of data signing, do not worry about data security issues
Technical realization:
- Dynamitey proxy implements the interface type
- DotNetty achieve TCP communication
- Object Binary Serialization
RPC calls
Server
var server = new ServerCreater().CreatetRPC(805); server.CheckSign(); server.SetSessionManage(new SessionManage()); server.Register<ITestService, TestService>(); server.Start();
CreateRPC is an extension method, get references CRL.RPC
SetSessionManage, custom Session storage, the default is memory
CheckSign When processing requests, the signature verification parameters
The client interface calls
var clientConnect = new RPCClientConnect("127.0.0.1", 805); clientConnect.UseSign(); var service = clientConnect.GetClient<ITestService>(); label1: service.Login(); Console.WriteLine("loginOk"); int? a = 1; string error; service.Test1(1, a, out error); Console.WriteLine("error:" + error); var obj2 = service.Test2(new TestObj() { Name = "test" }); Console.WriteLine("obj2:" + obj2.ToJson()); Console.ReadLine(); goto label1;
The client first call the login method to log and record token returned from the server
Test1 method token back to the server to verify the login status, and remote calls
When you call a UseSign method, the parameters will be submitted for signing, signature Press KEY same signature is compared to the login server returned TOKEN, server
Dynamic webApi
Also in the above structure, based on the definition of the interface is not pasted
Server definitions
var server = new ServerCreater().CreatetApi(); server.CheckSign(); server.SetSessionManage(new SessionManage()); server.Register<ITestService, TestService>(); was listener = new Server Listener (); //listener.Start("http://localhost:809/");// custom monitor
If the host is a .NET Web sites in web.config increase processing module
<system.webServer> <modules> <add name="DynamicModule" type="CRL.DynamicWebApi.DynamicModule" /> </modules> </system.webServer>
If it is a separate program, you can start ServerListener
The client calls
var clientConnect = new CRL.DynamicWebApi.ApiClientConnect("http://localhost:53065"); //var clientConnect = new CRL.DynamicWebApi.ApiClientConnect("http://localhost:8022"); clientConnect.UseSign(); var service = clientConnect.GetClient<ITestService>(); label1: service.Login(); Console.WriteLine("loginOk"); int? a = 1; string error; service.Test1(1, a, out error); Console.WriteLine("error:" + error); var obj2 = service.Test2(new TestObj() { Name = "test" }); Console.WriteLine("obj2:" + obj2.ToJson()); Console.ReadLine(); goto label1;
WebSocket
WebSocket is a rather special way, often used for duplex communication way, the client can send the number to the server, the server can send data to the client
Remove the server to the client send data, in the form of interface calls can also be implemented, where the server sends data to the client, the client uses a subscription-way
Service-side implementation
var server = new ServerCreater().CreatetWebSocket(8015); server.CheckSign(); server.SetSessionManage(new SessionManage()); server.Register<ITestService, TestService>(); server.Start(); new CRL.Core.ThreadWork().Start("send", () => { var socket = server.GetServer() as CRL.WebSocket.WebSocketServer; socket.SendMessage("hubro", new socketMsg() { name = DateTime.Now.ToString() }, out string error); Console.WriteLine("send msg"); return true; }, 10);
The above demo code, the server opens up a thread, timed to the client "hubro" send data socket.SendMessage
Client implementation
var clientConnect = new CRL.WebSocket.WebSocketClientConnect("127.0.0.1", 8015); clientConnect.UseSign(); clientConnect.SubscribeMessage<socketMsg>((obj) => { Console.WriteLine("OnMessage:" + obj.ToJson()); }); clientConnect.StartPing(); var service = clientConnect.GetClient<ITestService>(); label1: service.Login(); Console.WriteLine("loginOk"); int? a = 1; string error; service.Test1(1, a, out error); Console.WriteLine("error:" + error); var obj2 = service.Test2(new TestObj() { Name = "test" }); Console.WriteLine("obj2:" + obj2.ToJson()); Console.ReadLine(); goto label1;
clientConnect.SubscribeMessage the subscription message is, by way of subscription, the server sends data processing
You can see above in various forms, server implementation and client calls is basically the same, the interface can be reused defined as interface communications Leverage effect
Specific implementation details of the function and see the source code and demo, already open, please download
Source Address:
CRL:
https://github.com/hubro-xx/CRL5
RCP:
https://github.com/hubro-xx/CRL5/tree/master/RPC
WebAPI:
https://github.com/hubro-xx/CRL5/tree/master/DynamicWebApi
WebSocket:
https://github.com/hubro-xx/CRL5/tree/master/WebSocket