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