当前位置: 首页 > 工具软件 > api.yike.io > 使用案例 >

nodejs socket.io粗略分房间

卢树
2023-12-01

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!');
    })
});

 类似资料: