在线聊天室
信令服务器的搭建
// 前期准备
// 下载 指定版本的scoketio ,不然后续操作会报错
npm install socket.io@2.2.0
npm install log4js
'use strict'
var http = require('http') ;
var https = require('https') ;
var fs = require('fs') ;
var express = require('express') ;
var serverIndex = require('serve-index') ;
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(serverIndex('./public')) ;
app.use(express.static('./public')) ;
var http_server = http.createServer(app) ;
http_server.listen(80,'0.0.0.0') ;
var options = {
key:fs.readFileSync('./cert/cert.key'),
cert:fs.readFileSync('./cert/cert.pem')
}
var https_server = https.createServer(options,app) ;
var io = socketIo.listen(https_server) ;
io.sockets.on('connection',(socket)=>{
socket.on('message',(room,data)=>{
console.log('I come in server message') ;
socket.to(room).emit('message',room,data) ;
}) ;
socket.on('join',(room)=>{
socket.join(room) ;
var myRoom = io.sockets.adapter.rooms[room];
var users = Object.keys(myRoom.sockets).length ;
socket.emit('joined',room,socket.id) ;
}) ;
}) ;
https_server.listen(443,'0.0.0.0') ;
// 编写 html 、js 、css 文件 (具体步骤同前几篇文章)
<html>
<head>
<title>在线聊天室</title>
<link rel="stylesheet" href="./css/main.css"></link>
</head>
<body>
<table align="center">
<tr>
<td>
<label>UserName: </label>
<input type=text id="username"></input>
</td>
</tr>
<tr>
<td>
<label>room: </label>
<input type=text id="room"></input>
<button id="connect">Conect</button>
<button id="leave" disabled>Leave</button>
</td>
</tr>
<tr>
<td>
<label>Content: </label><br>
<textarea disabled style="line-height: 1.5;" id="output" rows="10" cols="100"></textarea>
</td>
</tr>
<tr>
<td>
<label>Input: </label><br>
<textarea disabled id="input" rows="3" cols="100"></textarea>
</td>
</tr>
<tr>
<td>
<button id="send">Send</button>
</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>
'use strict'
var userName = document.querySelector('input#username') ;
var inputRoom = document.querySelector('input#room') ;
var btnConnect = document.querySelector('button#connect') ;
var inputArea = document.querySelector('textarea#input') ;
var outArea = document.querySelector('textarea#output') ;
var btnSend = document.querySelector('button#send') ;
var socket ;
var room ;
btnConnect.onclick = ()=>{
socket = io.connect() ;
socket.on('joined',(room,id) =>{
console.log('I have joined this room') ;
btnConnect.disabled = true ;
btnSend.disabled = false ;
inputArea.disabled = false;
}) ;
socket.on('disconnect', (socket)=>{
btnConnect.disabled = false;
inputArea.disabled = true;
btnSend.disabled = true;
});
socket.on('message',(room,data)=>{
console.log('I successful !!!') ;
outArea.scrollTop = outArea.scrollHeight;
outArea.value = outArea.value + data + '\r';
}) ;
room = inputRoom.value ;
socket.emit('join',room) ;
}
btnSend.onclick = ()=>{
var data = inputArea.value ;
data = userName.value+':'+data ;
socket.emit('message',room,data) ;
inputArea.value ='' ;
}
button {
margin: 0 20px 25px 0;
vertical-align: top;
width: 134px;
}
div#getUserMedia {
padding: 0 0 8px 0;
}
div.input {
display: inline-block;
margin: 0 4px 0 0;
vertical-align: top;
width: 310px;
}
div.input > div {
margin: 0 0 20px 0;
vertical-align: top;
}
div.output {
background-color: #eee;
display: inline-block;
font-family: 'Inconsolata', 'Courier New', monospace;
font-size: 0.9em;
padding: 10px 10px 10px 25px;
position: relative;
top: 10px;
white-space: pre;
width: 270px;
}
section#statistics div {
display: inline-block;
font-family: 'Inconsolata', 'Courier New', monospace;
vertical-align: top;
width: 308px;
}
section#statistics div#senderStats {
margin: 0 20px 0 0;
}
section#constraints > div {
margin: 0 0 20px 0;
}
section#video > div {
display: inline-block;
margin: 0 20px 0 0;
vertical-align: top;
width: calc(50% - 22px);
}
section#video > div div {
font-size: 0.9em;
margin: 0 0 0.5em 0;
width: 320px;
}
h2 {
margin: 0 0 1em 0;
}
section#constraints label {
display: inline-block;
width: 156px;
}
section {
margin: 0 0 20px 0;
padding: 0 0 15px 0;
}
section#video {
width: calc(100% + 20px);
}
video {
--width: 90%;
display: inline-block;
width: var(--width);
height: calc(var(--width) * 0.75);
margin: 0 0 10px 0;
}
@media screen and (max-width: 720px) {
button {
font-weight: 500;
height: 56px;
line-height: 1.3em;
width: 90px;
}
div#getUserMedia {
padding: 0 0 40px 0;
}
section#statistics div {
width: calc(50% - 14px);
}
video {
display: inline-block;
width: var(--width);
height: 96px;
}
}
效果图:
|