To send data from the server to the client, it does not matter whether it is the web, mobile or desktop, there are enough technicians. But the problem is that they are all different and if you need to implement an alert about the same event for clients on all major platforms, you will have to duplicate this same alert code. That's why I want to share with the community my practice of working with a single product called LightStreamer .using System; using System.Collections; using System.Threading; using Lightstreamer.Interfaces.Data; public class HelloWorldAdapter : IDataProvider { private IItemEventListener _listener; private volatile bool go; public void Init(IDictionary parameters, string configFile) { } public bool IsSnapshotAvailable(string itemName) { return false; } public void SetListener(IItemEventListener eventListener) { _listener = eventListener; } public void Subscribe(string itemName) { if (itemName.Equals("greetings")) { new Thread(new ThreadStart(Run)).Start(); } } public void Unsubscribe(string itemName) { if (itemName.Equals("greetings")) { go = false; } } public void Run() { go = true; int c = 0; Random rand = new Random(); while (go) { IDictionary eventData = new Hashtable(); eventData["message"] = c % 2 == 0 ? "Hello" : "World"; eventData["timestamp"] = DateTime.Now.ToString("s"); _listener.Update("greetings", eventData, false); c++; Thread.Sleep(1000 + rand.Next(2000)); } } } using System; using System.Net.Sockets; using Lightstreamer.DotNet.Server; public class DataAdapterLauncher { public static void Main(string[] args) { string host = "localhost"; int reqrepPort = 6661; int notifPort = 6662; try { DataProviderServer server = new DataProviderServer(); server.Adapter = new HelloWorldAdapter(); TcpClient reqrepSocket = new TcpClient(host, reqrepPort); server.RequestStream = reqrepSocket.GetStream(); server.ReplyStream = reqrepSocket.GetStream(); TcpClient notifSocket = new TcpClient(host, notifPort); server.NotifyStream = notifSocket.GetStream(); server.Start(); System.Console.WriteLine("Remote Adapter connected to Lightstreamer Server."); System.Console.WriteLine("Ready to publish data..."); } catch (Exception e) { System.Console.WriteLine("Could not connect to Lightstreamer Server."); System.Console.WriteLine("Make sure Lightstreamer Server is started before this Adapter."); System.Console.WriteLine(e); } } } <?xml version="1.0"?> <adapters_conf id="HelloWorld"> <metadata_provider> <adapter_class>com.lightstreamer.adapters.metadata.LiteralBasedProvider</adapter_class> </metadata_provider> <data_provider> <adapter_class>com.lightstreamer.adapters.remote.data.RobustNetworkedDataProvider</adapter_class> <classloader>log-enabled</classloader> <param name="request_reply_port"> 6661</param> <param name="notify_port"> 6662</param> </data_provider> </adapters_conf> <html> <head> <script src="http://cdnjs.cloudflare.com/ajax/libs/require.js/1.0.7/require.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.0.min.js"></script> <script src="C:/Lightstreamer/DOCS-SDKs/sdk_client_javascript/lib/lightstreamer.js"></script> <script> require(["LightstreamerClient","Subscription","StaticGrid"],function(LightstreamerClient,Subscription,StaticGrid) { var lsClient = new LightstreamerClient("http://localhost:8080", "HelloWorld"); lsClient.connect(); var lsSubscription = new Subscription("MERGE",['greetings'],['greetings', 'message', 'timestamp']); lsSubscription.addListener({ onItemUpdate: function(updateObject) { $("#message").text('Message=' + updateObject.getValue("message")); $("#timestamp").text('TimeStamp=' + updateObject.getValue("timestamp")); } }); lsClient.subscribe(lsSubscription); }); </script> </head> <body> <span id="message"></span><br/> <span id="timestamp"> </span> </body> </html> 
new Subscription("MERGE",['userID123'],['greetings', 'message', 'timestamp']) Source: https://habr.com/ru/post/220343/
All Articles