logger.debug(‘the number of user in room is:’+users)
//处理一对一通信
if (users //给本人回信息 socket.emit(‘joined’,room,socket.id); if(users>1){ socket.to(room).emit(‘otherjoin’,room,socket.id); } }else{ socket.leave(room); socket.emit(‘full’,room,socket.id); } //给本人回信息 //socket.emit(‘joined’,room,socket.id); //给房间除自己以外所有人回 // socket.to(room).emit(‘joined’,room,socket.id); //给房间所有人回 // io.in(room).emit(‘joined’,room,socket.id); //除自己所有站点回 // socket.broadcast.emit(‘joined’,room,socket.id) }); //用户离开 socket.on(‘leave’,(room)=>{ var myRoom = io.sockets.adapter.rooms[room]; var users =(myRoom)?Object.keys(myRoom.sockets).length:0; //users-1 logger.debug(‘the number of user in room is:’+(users-1)); socket.to(room).emit(‘bye’,room,socket.id); socket.emit(‘leaved’,room,socket.id); //给本人回信息 //socket.emit(‘leaved’,room,socket.id); //给房间除自己以外所有人回 //socket.to(room).emit(‘leaved’,room,socket.id) //给房间所有人回 //io.in(room).emit(‘leaved’,room,socket.id); //除自己所有站点回 //socket.broadcast.emit(‘leaved’,room,socket.id) }); }); ‘use strict’ var http = require(‘http’); var https = require(‘https’); var fs = require(‘fs’); var express= require(‘express’); var serveIndex=require(‘serve-index’); var USERCOUNT=3; //命令服务器 var socketIo =require(‘socket.io’); var log4js = require(‘log4js’); log4js.configure({ appenders:{ file:{ type:‘file’, filename:‘app.log’, layout:{ type:‘pattern’, pattern:’%r %p - %m’, } } }, categories:{ default:{ appenders:[‘file’], level:‘debug’ } } }); var logger = log4js.getLogger(); var app=express(); app.use(serveIndex(’./public’)); app.use(express.static(’./public’)); //http server var http_server=http.createServer(app); //-----------------------------------------------------------------------// var options={ key:fs.readFileSync(’./cert/3435783_huangxiaoguo.club.key’), cert:fs.readFileSync(’./cert/3435783_huangxiaoguo.club.pem’) } //https server var https_server=https.createServer(options,app); //命令服务器绑定https var io = socketIo.listen(https_server); io.sockets.on(‘connection’,(socket)=>{ logger.debug(“connection”); //转发信息 socket.on(‘message’, (room, data)=>{ logger.debug("message data “+socket.id+” "+room,data); socket.to(room).emit(‘message’, room, socket.id, data)//房间内所有人,除自己外 }); //用户加入 socket.on(‘join’,(room)=>{ logger.debug(“join”,",room = “, room,”,socket.id = ", socket.id); socket.join(room); var myRoom = io.sockets.adapter.rooms[room]; var users =(myRoom)?Object.keys(myRoom.sockets).length:0; logger.debug(‘the number of user in room is:’+users) //处理一对一通信 if (users //给本人回信息 socket.emit(‘joined’,room,socket.id); if(users>1){ socket.to(room).emit(‘otherjoin’,room,socket.id); } }else{ socket.leave(room); socket.emit(‘full’,room,socket.id); } //给本人回信息 //socket.emit(‘joined’,room,socket.id); //给房间除自己以外所有人回 // socket.to(room).emit(‘joined’,room,socket.id); //给房间所有人回 // io.in(room).emit(‘joined’,room,socket.id); //除自己所有站点回 // socket.broadcast.emit(‘joined’,room,socket.id) }); //用户离开 socket.on(‘leave’,(room)=>{ var myRoom = io.sockets.adapter.rooms[room]; var users =(myRoom)?Object.keys(myRoom.sockets).length:0; //users-1 logger.debug(‘the number of user in room is:’+(users-1)); socket.to(room).emit(‘bye’,room,socket.id); socket.emit(‘leaved’,room,socket.id); //给本人回信息 //socket.emit(‘leaved’,room,socket.id); //给房间除自己以外所有人回 //socket.to(room).emit(‘leaved’,room,socket.id) //给房间所有人回 //io.in(room).emit(‘leaved’,room,socket.id); //除自己所有站点回 //socket.broadcast.emit(‘leaved’,room,socket.id) }); }); https_server.listen(443, ‘0.0.0.0’); http_server.listen(80,‘0.0.0.0’); 利用socketio实现简单聊天室 Chat Room UserName: 以薪资待遇为基础,以发展为最终目标,要在高薪资的地方,谋求最好的发展! 下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。有**Android架构视频+BATJ面试专题PDF+核心笔记等资料。希望能够帮助到大家提升技术。如果大家想要获取的话,可以私信我【666】免费获取哦** 以薪资待遇为基础,以发展为最终目标,要在高薪资的地方,谋求最好的发展! 下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。有**Android架构视频+BATJ面试专题PDF+核心笔记等资料。希望能够帮助到大家提升技术。如果大家想要获取的话,可以私信我【666】免费获取哦** [外链图片转存中…(img-vyKoKqP1-1644921132745)]
尾声