C # Data push live data push lightweight message subscription publish multilevel message push distributed push

Source: Internet
Author: User

Preface

This article will use a NuGet exposed component technology to achieve data subscription push function, pushed data by the server, the client subscribes to the specified data, that can receive the server pushed over the data, including the automatic re-connect function, easy to use

NuGet Address: https://www.nuget.org/packages/HslCommunication/

GitHub Address: Https://github.com/dathlin/HslCommunication If you like star or fork, you can also enjoy support.

You can download the installation in the NuGet Manager in Visual Studio, or you can enter the following instructions directly in the NuGet console to install:

Install-package hslcommunication

NuGet Installation Tutorial Http://www.cnblogs.com/dathlin/p/7705014.html

Technical Support QQ Group: 592132877 (Component version update details will be released in the group first) Component API address: http://www.cnblogs.com/dathlin/p/7703805.html

Access to the test project, both the client and the server source code are in the address above

The following project is an Access test project for this component, you can test the initial access, eliminating the hassle of writing your test program, and the source code is in the project at the beginning of this article.

is:hslcommunicationdemo.zip

The following is a subscription to the data for the specified keyword for the server, which provides several keyword subscriptions for testing.

    • B Random Number Subscription
    • C Time Subscription
    • D JSON data Subscription
    • E-XML Data subscription

If the server is not available, download the local version of the server

Pushnetserver.zip

Feature Support
    • Support for data broadcast, broadcast by specified keywords
    • Support for client wire break and re-connect
    • Supports simple server-side monitoring
    • Support Log output
    • Server supports data input from other remote servers to form a data push network
    • Currently only supports string data pushes, complex data needs to be serialized, with reference to JSON push and XML push

Reference

Modbus components all the function classes are in the hslcommunication.enthernet namespace, so before you use Add

Using hslcommunication.enthernet;using hslcommunication;

  

Server-side Setup

Declaring a variable first

Private Netpushserver Pushserver;

And then write the following code in a Startup button.

Pushserver = new Netpushserver ();p Ushserver.serverstart (12345);

Up to this point, the server code has been basically written, just two or three lines of code. Only the current data does not have the data push function.

What a classic scenario is, for example, you have a bunch of real-time data collected from the device, and the data is on the server. You also have a client software, many people are online at the same time, if others turn on the monitoring of real-time data, it is particularly suitable for this component of the method, because you go to the database is obviously slower, and the pressure on the server is also large. This component is the perfect solution to this issue,

Suppose you collect data from the device, we make a 1 second timer, then start the timer, simulate the data push per second, push what? Try random numbers first.

Private random random = new random (); First define a random number generator

  

The code for the timer is as follows:

Pushserver.pushstring ("A", random. Next (1000, 10000). ToString ());

So far, a complete code for the server has been written. Let's see how the client writes.

Client Side Build

Declaring a variable first

Private Netpushclient pushclient;

Instantiate, you need to specify the IP and port of the server, and the keywords you want to subscribe to

Pushclient = new Netpushclient ("127.0.0.1", 12345, "A");

Next is to create a subscription, you need to specify a call to the delegate, will return a successful signal, because there might be a reason for the network, it is possible because there is no such keyword, will cause the creation to fail.

Operateresult Create = Pushclient.createpush (new action<netpushclient, string> (Pushfromserver));            if (create. issuccess)            {                MessageBox.Show ("Success");            }            else            {                MessageBox.Show ("failed:" + Create. Message);            }

The above uses a delegate, the Pushfromserver method is as follows, mainly uses the delegate to display the data:

private void Pushfromserver (Netpushclient pushclient, string data)        {            if (ishandlecreated) Invoke (New action< ;string> (M =              {                  Label8. Text = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss.fff"); Show the receiving time                  receivecount++;                  Label9. Text = Receivecount.tostring ();  Shows the number of received                  Textbox4.text = m;  Data is finally shown here              });        }

Shut down the client before the corresponding window is closed. If you do not close the window manually, it is likely that the connection will still be there.

Pushclient.closepush ();

  

A simple complete data subscription has been completed.

Complex Data subscriptions

The above code can only push string data, how to implement the push of complex data, we assume that there are 4 of data, we need to push to the client at the same time. Then we can choose 2 ways, one is JSON, one is XML, each has pros and cons

Json

            Jobject json = new Jobject ();            Json. ADD ("Value1", New Jvalue (random. Next (9999));            Json. ADD ("value2", New Jvalue (Math.Round (random. Nextdouble (), 6) *);            Json. ADD ("Value3", New Jvalue (Guid.NewGuid (). ToString ()));            Json. ADD ("Value4", New Jvalue (DateTime.Now));            Pushserver.pushstring (Textbox14.text, JSON. ToString ());

Xml

            XElement element = new XElement ("Data");            Element. Setelementvalue ("value1", random. Next (9999));            Element. Setelementvalue ("value2", (Math.Round (random. Nextdouble (), 6) * 1000). ToString ());            Element. Setelementvalue ("Value3", Guid.NewGuid (). ToString ());            Element. Setelementvalue ("Value4", DateTime.Now.ToString ("O"));            Pushserver.pushstring (Textbox18.text, element. ToString ());

Parse the time according to the corresponding information can be resolved.

Token settings

The server supports token settings, and if the client fails to provide a consistent token checksum, the connection is denied and the security of the system is improved

Pushserver.token = new Guid ("04f6e588-4b9c-4dfb-86b2-4389742534b5");  

The client needs to set the same token to log on to the server for data validation.

Log output

Pushserver.lognet = new HslCommunication.LogNet.LogNetSingle ("Log.txt");       Support Log

  

Monitor the number of online clients

There is a property that identifies the number of all clients on the server side of the line, a convenient system for monitoring

Pushserver.onlinecount

Star-shaped data network

The above shows are manually push server data, or write code acquisition device to achieve automatic push, if you want to do distributed data push, or to alleviate the server's data push, then you can do as follows

That is, a master server pushes data to 2 sub-servers, and then pushes the data to the client by the child server.

For the main service and push client code does not need any change, mainly with a layer of intermediate server, is also the Netpushserver class, the child server data needs to come from manual, only need to manually add a line of code to

Operateresult Create = Pushserver.createpushremote ("127.0.0.1", 12345, "A");                if (create. issuccess)                {                    MessageBox.Show ("created successfully! " );                }                else                {                    MessageBox.Show ("Create failed! "+ Create. Message);                }

This means that the child server subscribes to the data of the keyword A of another primary server and pushes it to other online clients in real time.

Once you are familiar with this principle, you can build a high-performance data push Network service.

Dynamic keywords

The above operation is static keyword push, push the data are everyone has, how to push a dedicated client? can also be implemented, a little more complex, first through another network to contract a common unique keyword, such as the GUID code, and then has this code as the keyword push.

Another interactive network can be consulted: http://www.cnblogs.com/dathlin/p/7697782.html

What are the examples of this situation,,, such as you make a request to the server, this request may be long, you do not know when the server can be completed, a percentage of the server to show the progress of the time, the need for dynamic keywords, the process of server operation only need to push progress to the unique keyword.

If there is any problem, you can add group chat.

C # Data push live data push lightweight message subscription publish multilevel message push distributed push

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.