Get the App
SLTechnology News&Howtos  ›  Development  › 

.net how to use SuperSocket Framework to implement WebSocket Front end

Shulou Source: shulou.com Published: 2022-06-02 01:11:48 09月21日 Update

This article will explain in detail how .net uses the SuperSocket framework to achieve the WebSocket front-end. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In fact, the front-end implementation is relatively easy, because we have JavaScript WebSocket Api, which looks something like this:

Var ws =''; / / WebSocket object var userName = 'test classmate' + parseInt (Math.random (1) * 888); / / username / / initialize connection function connect () {var address = "ws://127.0.0.1:10086"; / / server address ws = new WebSocket (address) / / instantiate the WebSocket object / / start the connection with ws.onopen = function (e) {ws.send ('[login] {"userid": "'+ userName +'"}');}; / / ws.onmessage = function (e) {var Json = eval ('('+ e.data +')') when receiving the message Switch (Json.type) {case '1customers: / / break; case' 2clients: var Html =''; Html + = 'when new users connect

'; Html + = Json.user +':'+ Json.msg; Html + ='

'; $(' # msgBox') .append (Html); break;}}; / / ws.onerror = function (e) {} when an error occurs; / / ws.onclose = function (e) {$('# msgBox') {$('# msgBox'). Append (') when the connection is closed

The connection to the chat room has been disconnected.

Send function send () {var SendText = $('# send'). Val (); ws.send ('[send] {"msg": "'+ SendText +'", "user": "'+ userName +'"}');}

These things are fixed, just remember ~ give a simple explanation:

One of the keys: ws.onmessage, which responds to a message from the server-and then you can do whatever you want.

The information returned by this method is stored in e.data, depending on how your server writes it (here it is seen as returning Json).

The second key: ws.send (), which sends messages to the server you write (as for content, it still depends on how your back end implements it).

The third key: ws.onopen (), through which you can open communication with the server (set up the WebSocket object).

Here's how it works (don't complain about being ugly):

Remember to find a browser that supports H5.

This is the end of this article on ".net how to use SuperSocket framework to achieve WebSocket front-end". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Service information front-end key content object article framework depends on more user good practical do whatever you want thing classmate address example effect article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Docker macOS Shulou Technology Redmi