SignalR self-hosted, SignalR boarding
Source code
https://github.com/xlb378917466/Chat.Server
1. Use the console program to host the SignalR server. This requires the use of Owin middleware,
2. Implement a server-side ChatHub
public class ChatHub : Hub { public void Send(string name, string message) { Clients.All.addMessage(name, message); } public override Task OnConnected() { Console.WriteLine("Client connected: " + Context.ConnectionId); return base.OnConnected(); } public override Task OnDisconnected(bool stopCalled) { Console.WriteLine("Client disconnected: " + Context.ConnectionId); return base.OnDisconnected(stopCalled); } }
3. Build a Chat. WebClient that can communicate through the web page. In this example, you can solve the problem of reconnection.
This placeEasily encountered problemsYes,
A connection error occurs because the SignalR of the server is inconsistent with the client version,
The other is the big and lowercase case in js. For example, the HUB on the server is ChatHub, but only $. connection. chatHub can be used in js. Otherwise, an error is returned.
4. To build a CS communication Client, you need to use Microsoft. AspNet. SignalR. Client
Build a proxy through Microsoft. AspNet. SignalR. Client and send messages through the proxy
Signalr MSDN Technical Documentation