websocket
前端 示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <script>
var input =document.querySelector('input') var button =document.querySelector('button') var div =document.querySelector('div')
var socket =new WebSocket('ws://echo.websocket.org') socket.addEventListener('open',function(){ div.innerHTML='连接服务成功了' })
button.addEventListener('click',function(){ var val =input.value socket.send(val) })
socket.addEventListener('message',function(e){ console.log(e.data) div.innerHTML=e.data })
socket.addEventListener('close',function(){ div.innerHTML='服务断开连接' })
</script>
|
点击调试工具 中的 NetWork 中的 WS 可以看到发送和接收情况
绿色是发送给服务器的
红色是服务器返回来的
服务器端代码
1.安装 nodejs-websocket
yarn add nodejs-websocket
2.示例代码
const ws = require(‘nodejs-websocket’)
const PORT = 3000
// 创建一个server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| const server = ws.createServer((connect) => { console.log('有用户连接上来了') connect.on('text', (data) => { console.log('接收到了用户的数据', data) connect.send(data.toUpperCase() + '!!!') })
connect.on('close', () => { console.log('连接断开了') }) connect.on('error', () => { console.log('用户连接异常') }) })
server.listen(PORT, () => { console.log('websocktet服务启动成功了,监听了端口' + PORT) })
|
一个简单的聊天室
前端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <script> const type_enter = 0 const type_leave = 1 const type_message = 2 var input = document.querySelector('input') var button = document.querySelector('button') var div = document.querySelector('div')
var socket = new WebSocket('ws://localhost:3000')
socket.addEventListener('open', function () { })
button.addEventListener('click', function () { var val = input.value socket.send(val) input.value = '' })
socket.addEventListener('message', function (e) { let data = JSON.parse(e.data) var dv = document.createElement('div') dv.innerText = data.msg + '---------' + data.time if (data.type == type_enter) { dv.style.color = 'green' } else if (data.type == type_leave) { dv.style.color = 'red' } else { dv.style.color = '#333' } div.appendChild(dv) })
socket.addEventListener('close', function () { }) </script>
|
服务端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| const ws = require('nodejs-websocket')
const PORT = 3000
let count = 0 const type_enter = 0 const type_leave = 1 const type_message = 2
const server = ws.createServer((connect) => { count++ connect.userName = `用户${count}` broadcast({ type: type_enter, msg: `${connect.userName}进入了聊天室!`, time: new Date().toLocaleDateString() }) connect.on('text', (data) => { broadcast({ type: type_message, msg:data, time: new Date().toLocaleDateString() }) })
connect.on('close', () => { count-- broadcast({ type: type_leave, msg: `${connect.userName}离开了聊天室!`, time: new Date().toLocaleDateString() }) }) connect.on('error', () => { console.log('用户连接异常') }) })
function broadcast(msg) { server.connections.forEach((item) => { item.send(JSON.stringify(msg)) }) }
server.listen(PORT, () => { console.log('websocktet服务启动成功了,监听了端口' + PORT) })
|