nodejs socket.io粗略分房间

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="socket.io.js"></script>
</head>
<body>
    <h1>Echo Test</h1>
    <input id="sendTxt" type="text" />
    <button id='sentBtn'>发送</button>
    <div id="recv"></div>
</body>
<script>
    var room;
    function showMessage(str,type,number) {
        var div = document.createElement('div');
        div.innerHTML = str;
        if (type == "enter") {
            div.style.color = "blue";
        }else if (type == "leave"){
            div.style.color = "red";
        }
        document.body.appendChild(div);
    }
    document.getElementById("sentBtn").onclick = function () {
        var txt = document.getElementById("sendTxt").value;
        if (txt) {
            socket.emit('message', {
                msg: txt,
                room: room
            });
        }
    };

    var socket = io('http://localhost:8001');
    socket.emit('enter', {
        t: 1,
        token: '850b11479b2f846f0e72a262a246e7e7'
    });
    socket.on('enter', function (data) {
        room = data.room;
        showMessage(data.msg, 'enter');
    });
    socket.on('message', function (data) {
        showMessage(data, 'message',room);
    });
    socket.on('disconnect', function (data) {
        showMessage(data, 'leave');
    });
</script>
</html>

nodejs:

var http = require('http');
var querystring =require('querystring');
var app = require('http').createServer();
var io = require('socket.io')(app);
var room;
app.listen(8001);
io.on('connection', function (socket) {
    socket.on('enter',function (data) {
        var type = data.t;
        if(parseInt(type) == 1){
            var post_data = querystring.stringify({
                token:data.token
            });
            var options = {
                hostname:'yike',     //此处不能写协议,如 : http://,https://  否则会报错
                port:80,
                path:'/api/user/get_id',
                method:'POST',
                headers: {
                    'Content-Type':'application/x-www-form-urlencoded',
                    'Content-Length': post_data.length
                }
            };
            var req = http.request(options,function(res){
                res.setEncoding('utf8');
                res.on('data',function(chunk){
                    chunk = JSON.parse(chunk);
                    room = chunk.id
                });
            });
            req.write(post_data);
            req.end();
        }else {
            var post_data = querystring.stringify({
                'token1':data.token1,
                'token2':data.token2
            });
            var options = {
                hostname:'yike',     //此处不能写协议,如 : http://,https://  否则会报错
                port:80,
                path:'/api/user/get_two_id',
                method:'POST',
                headers: {
                    'Content-Type':'application/x-www-form-urlencoded',
                    'Content-Length': post_data.length
                }
            };
            var req = http.request(options,function(res){
                res.setEncoding('utf8');
                res.on('data',function(chunk){
                    chunk = JSON.parse(chunk);
                    room = chunk.id
                });
            });
            req.write(post_data);
            req.end();
        }

        socket.join(room);
        io.sockets.in(room).emit('enter',{
            msg: '加入房间',
            room: room
        });
    });
    socket.on('message',function (data) {
        io.sockets.in(data.room).emit('message',data.msg);
    });
    socket.on('disconnect', function (data) {
        socket.emit('leave','leave!');
    })
});

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_我走路带风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值