webRTC(六):webrtc信令服务器实现

信令服务器实现

  • 引入socketIo和log4js
//命令服务器
var socketIo =require('socket.io');
var log4js = require('log4js');

安装 socketIo和日志log4js插件

/home/huangxiaoguo/RTCWorkSpace/samples/webserver 

npm install socket.io 

npm install  log4js 
  • 命令服务器绑定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<USERCOUNT) {
			//给本人回信息
			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<USERCOUNT) {
			//给本人回信息
			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实现简单聊天室

  • 页面
<html>
	<head>
		<title>
			Chat Room
		title>
	head>
	<body>
		<table align="center">
			<tr>
				<td>
					<label>UserName:label>
					<input type="text" id="uesrname"/>
				td>	
			tr>
			<tr>
				<td>
					<label>room:label>
					<input type="text" id="room"/>
					<button id="connect">connectbutton>
				td>
			tr>
			<tr>
				<td>
					<label>Content:label><br>
					<textarea disabled  id="output" rows="10" cols="50">textarea>
				td>
			tr>
			<tr>
				<td>
					<label>Input:label><br>
					<textarea disabled  id="input" rows="3" cols="50">textarea>
				td>
			tr>
			<tr>
				<td>
					<button id="send">Sendbutton>
				td>
			tr>
		table>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js">script>
		<script src="./js/client.js">script>
	body>
html>
  • js
'use strict'

var userName= document.querySelector('input#uesrname');
var inputRoom= document.querySelector('input#room');
var btnConnect= document.querySelector('button#connect');
var outputArea= document.querySelector('textarea#output');
var inputArea= document.querySelector('textarea#input');
var btnSend= document.querySelector('button#send');


var socket ;
var room;
btnConnect.onclick=()=>{
	
	//链接服务器
	socket = io.connect();
	//断开服务器
	socket.on('disconnect', socket=> {
		console.log("disconnect===>"+socket);
	})
	//消息
	socket.on('joined',(room,id)=>{
		btnConnect.disabled=true;
		inputArea.disabled=false;
		btnSend.disabled=false;
	});
	socket.on('leaved',(room,id)=>{
		btnConnect.disabled=false;
		inputArea.disabled=true;
		btnSend.disabled=true;
	});
	socket.on('message',(room,id,data)=>{
		console.log("data====>"+data);
		outputArea.scrollTop = outputArea.scrollHeight;//窗口总是显示最后的内容
		outputArea.value=outputArea.value+data+'\r';
	});
	//发送消息
	room =inputRoom.value;
	socket.emit('join',room);
}

btnSend.onclick=()=>{
	if (inputArea.value==='') {
		return;
	} 
	var data=inputArea.value;
	data=userName.value+':'+data;
	socket.emit('message',room,data);
	outputArea.scrollTop = outputArea.scrollHeight;//窗口总是显示最后的内容
	outputArea.value=outputArea.value+data+'\r';
	inputArea.value='';
}

测试地址:https://www.huangxiaoguo.club/chatroom/index.html

你可能感兴趣的:(webRTC)