ASP. NET MVC with Nodejs for push

Source: Internet
Author: User
Tags emit

I've always wanted to make a real-time communication feature on the Web, and until recently, Nodejs was a good choice.

Here does not consider the beauty of the problem, a simple input push message, to ensure that the client can receive the push message,

Service side, simple code, refer to the following:

Introducing the HTTP module var http = require (' http ');//port number var port = Process.env.port | | 1337;//introduces Express module var Express = require (' Express '), App = Express (), Server = Http.createserver (App), Io = Requir E (' Socket.io '). Listen (Server); Introduce Socket.io module and bind to Server Server.listen (Port),//Output service start Console.log (' server started ');//Current online number var onlinecount = 0;var      Messages = [];//socket part io.on (' Connection ', function (socket) {//overlay current online number onlinecount++;    Console.log (' Current number of users: ' + onlinecount); Client disconnects Socket.on (' Disconnect ', function () {if (Onlinecount > 0) {//current online user minus one o            nlinecount--;        Console.log (' Current number of users: ' + onlinecount);    }    });            Receive data from the client Socket.on (' Fromwebclient ', function (webclientdata) {console.log (webclientdata);            Messages.push (Webclientdata);        Socket.broadcast.emit (' Pushtowebclient ', {hello:webclientdata});    }); Push client//socket.emit (' pushtowebclient ', {hello: ' world '});    Messages.foreach (function (msg) {socket.send (msg); });});

  

Client, simple code reference:

 1  var  socket = Io.connect ( '  
View Code

Push the party as follows:

$ ("#btnSend")            . Click (function () {                var msg = $ ("# sendmsg"). Val ();                 var socket = io.connect ('127.0.0.1:1337');                Socket.emit ('fromwebclient', msg);            });

ASP. NET MVC with Nodejs for 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.