即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

nodejs–socket.io

编程语言 dongmelon 23℃ 0评论

html

/**
 * Created by 18468 on 2017/5/18.
 */
var http=require("http");
var count = 0;
var server1 = http.createServer(function (req,res){
}).listen(3001,"127.0.0.1");
var server2 = http.createServer(function (req,res){
}).listen(3002,"127.0.0.1");
var io = require('socket.io').listen(server2);
io.sockets.on('connection',function(socket){
    count++;
    console.log('User connected');
    console.log(count)
    socket.emit('message',{text:'你上线了'});
    socket.broadcast.emit('message','你的好某XXX上线了');
    socket.on('disconnect',function(){
        count--;
        console.log(count)
        console.log('User disconnected');
    });
    socket.on("cd",function(data){
        console.log(data)
    })
});



nodejs

/**
 * Created by 18468 on 2017/5/18.
 */
var http=require("http");
var count = 0;
var server1 = http.createServer(function (req,res){
}).listen(3001,"127.0.0.1");
var server2 = http.createServer(function (req,res){   //这样好像可以分开端口
}).listen(3002,"127.0.0.1");
var io = require('socket.io').listen(server2);
io.sockets.on('connection',function(socket){
    count++;
    console.log('User connected');
    console.log(count)
    socket.emit('message',{text:'你上线了'});
    socket.broadcast.emit('message','你的好某XXX上线了');
    socket.on('disconnect',function(){
        count--;
        console.log(count)
        console.log('User disconnected');
    });
    socket.on("cd",function(data){
        console.log(data)
    })
});





转载请注明:CodingBlog » nodejs–socket.io

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情