C#socketio4netclient Access Node JS

Source: Internet
Author: User

Label:

The WebSocket protocol is not to be missed when it comes to node. It is perfectly matched to node, with two reasons.

The 1.WebSocket client time-based programming model is similar to custom events in node.

The 2.WebSocket enables long connections between the client and the server, and the node event-driven approach is good at maintaining high concurrent connections with a large number of clients.

----"The Nodejs of the layman"

Resources

GIT Nodejs-socketio-chat

GIT Socket.io

Socketio4netclient

In the learning process of node, What interests me most is the Socket.io module, in Git also has a lot of Daniel do the access module between node, feel that the general difference is not much, some of the difference lies in the use of methods and comfort, but after a lot of well-known modules in node, the code cleanliness has made the habit of logic programming people feel very comfortable, and in the appropriate When using parallelism according to your own will, this logic brings pleasure, and sometimes it's a great thing for developers.

Again, when using VS to connect to the node server, the use of the Socket.io module was a bit of a struggle, studied for two days and found Socketio4netclient online. After compiling a DLL file that can be referenced directly in the project, it is automatically parsed in the unity client at the root of the project, but when unity is published to a different platform, the solution is to import the project source into the Unity project. After Unity is compiled, it can be published to each platform to run.

Socket.ionode Service Side

First install the Socket.io module on the command line, such as using the Times error "not found module Socket.io" in the project, then to execute $NPM install Socket.io in the project directory, the node_modules generated in the project root directory The Socket.io module is added and successfully referenced.

varApp = require ('Express')();varHTTP = require ('http'). Server (app);varIO = require ('Socket.io') (HTTP);app.Get('/', Function (req, res) {Res.end ('Welcome to Socket Demo');}); Http.listen ( the, function () {Console.log ('server localhost:3000 is start');});

For ease of configuration, the express frame is used here, which indicates that the module can be installed like the Socket.io module by installing Express.

In the code, when the root directory is accessed, the string Welcome to socket demo is returned, and node listens on port 3000.

Io.on ('connection', function (socket) {    Socket.on ('message  ', function (obj) {        io.emit ('message', obj);     });});

The above code can be understood as, when the server receives a name of connection access, listens to the message event in the socket and pushes the passed obj parameter to the client.

C # Client

First add the DLL file in the source code to the project reference

{
New Client ("http://localhost:3000"); + = clien_opened ; + = clien_message ; + = clien_socketconnectionclosed ; + = Clien_error;

Socket. On ("Connection", (fn) = =
{
Console.WriteLine ("Connect enevt start");

Socket. Emit ("Messgae", Rejson);
});


Socket. Connect ();
}

void Clien_error (object sender, ErrorEventArgs e)
{
Console.WriteLine ("A Error Show" +e.message);
}

void Clien_socketconnectionclosed (object sender, EventArgs e)
{
Console.WriteLine ("Socket Connection succeed!");
}

void Clien_message (object sender, Messageeventargs e)
{
if (E! = null && e.message.event = = "Message")
{

}
}

void Clien_opened (object sender, EventArgs e)
{
Console.WriteLine ("Socket was open ...");
}

C#socketio4netclient Access Node JS

Related Article

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.

Tags Index: