node.js SOCKET 实时通信

//客户端---------------------



    
    Document


    

实时通讯


//服务器端---------------------------
var http = require("http");
var fs = require("fs");
var io = require("socket.io");

var server = http.createServer((req,res)=>{
    if(req.url=="/"){
        fs.readFile("index.html",(err,data)=>{
            res.end(data);
        })
    }else {
        res.end("no page found");
    }
})

server.listen(3000);

var ioClass = require("socket.io");
var ioInstanct = ioClass(server);

ioInstanct.on("connection",(socket)=>{
    socket.on("sendMsg",(data)=>{
        console.log("dada:",data);
        ioInstanct.emit("myNews","服务器转发:"+data);
    });

})

你可能感兴趣的:(node.js SOCKET 实时通信)