I. Background of the problem
At present, the company needs the program and the front-end part of the Internet product to carry on a real-time interaction, after a certain degree of selection, decided to use the Socket.io framework to carry out a practice, is a new attempt of the company, but also to increase the growth of their own knowledge, because I am doing back-end and desktop program development, So the front-end part is not a fine chat, mainly for the desktop program how to connect Socket.io to make an attempt
Second, the basic logic diagram
Local application: Deployed in LAN (can be connected to external network), mainly responsible for related data acquisition
Web browser-side: Not on the same network as the local application, requesting an initiator
Transit Service (priority): Transit service integrated authentication function, must be authenticated to the client to allow connection, and for the client to have a unique ID for the connection operation
Overall thinking, such as the above flowchart, initiated by the Web browser side of the request, the Web browser by connecting the transfer service, the information sent to the specified local application, the application processing is completed, the results through the original path for a feedback
Second, the client program development
The Client SDK uses socketioclientdotnet, an open source on GitHub, a client that can connect to Socket.io, support. NET 3.5 and above, and can help us with the development of the connection Socket.io easily.
Socket socket = IO. Socket ("ws://192.168.7.4:3000", New IO. Options () {reconnection = true, Cookies = new dictionary<string, string> () {"CompanyID", GlobalStatic.COMPANY.COMPANY_ID}, {"UserId", GlobalStatic.UserDomain.user.USER_ID}}, Timeout = 60000, Reconnectiondelay = $, transports = new list<string> () {"Websocke T "}, Path =" Heart "}); Socket. On (Socket.event_connect, () = {Jobject Jobject = Jobject.parse (Newtonsoft.Json.JsonConvert. SerializeObject (New {CompanyID = GlobalStatic.COMPANY.COMPANY_ID, to Ken = Globalstatic.token, hostId = globalstatic.host_id, HostName = Globalstatic.hos T_name, printernames = GlobalStatic.HOSTINFO.PrinterName, Source = "Assistant", UseRId = GlobalStatic.UserDomain.user.USER_ID}); Socket. Emit ("Authentication", jobject); Debug.WriteLine ("Authentication"); }); Socket. On (Socket.event_disconnect, (data) = {Debug.WriteLine ("Heartbeat disconnect" + data); Flag = false; Onconnectionstatus?. Invoke (flag); Socket. Disconnect (); }); Socket. On ("Authenticated", (obj) = = {flag = true; Onconnectionstatus?. Invoke (flag); Debug.WriteLine ("Heartbeat Authentication succeeded:" + obj); }); Socket. On ("Unauthorized", (obj) = {Debug.WriteLine ("Heartbeat authentication failed" + obj); Socket. Disconnect (); });
Socketioclientdotnet Note the point:
- Emit data must be string or Jobject (in Newtonsoft.json)
- From a practical point of view, every on in the internal monitoring is to maintain a thread, all if you do not want to block, then in the method of on the need to start a thread to complete
- If you do not set autoconnect=false in the options, the socket will be successfully followed by the Connect action.
Iii. issues relating to the socketioclientdotnet pit
In the actual use of the process, accidentally found in the program after a long time, the number of threads will continue to increase, try to adjust the parameters are useless, and finally can only be helpless to track the source part, in the thread folder to find Heartbeat_ Net35.cs file, found that the Run method in the while has been unable to jump out, and registered dowork more and more, as shown below, causing more and more threads, the current solution is to comment out the code, there is no problem, if there is any know this code role, hope to inform, thank you
Using system;using system.componentmodel;using system.threading;namespace quobject.engineioclientdotnet.thread{ public class Heartbeat {private volatile bool gotheartbeat = false; Private BackgroundWorker heartbeattimer= new BackgroundWorker (); Private CancellationTokenSource ts; Private Heartbeat () {ts = new CancellationTokenSource (); } public static Heartbeat Start (Action ontimeout, int timeout) {Heartbeat Heartbeat = new HEARTB Eat (); Heartbeat. Run (ontimeout, timeout); return heartbeat; } public void Onheartbeat () {gotheartbeat = true; } private void Run (Action ontimeout, int timeout) {Heartbeattimer = new BackgroundWorker (); Heartbeattimer.dowork + = (s, e) = = {while (!ts. iscancellationrequested) {System.Threading.Thread.Sleep (Timeout); if (!gotheartbeat &&!ts. iscancellationrequested) {ontimeout (); Break } } }; Heartbeattimer.runworkerasync (); } public void Stop () {ts. Cancel (); } }}
Mango
Source: http://www.cnblogs.com/OMango/
About yourself: focus. NET desktop development and web background development, beginning to touch the Internet related services such as microservices, docket, etc.
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph of the statement, and in the article page obvious location, the original link if there is a problem, can mail ([email protected]) Consulting.
Using Socket.io essay records under. Net