html5 nodejs&websocket聊天demo
来源:互联网 发布:数据光盘 编辑:程序博客网 时间:2024/06/04 18:01
首先需要安装nodejs
然后安装 npm install nodejs-websocket
以上为nodejs基础知识如有疑问可以查阅相关资料。
首先创建ws.js放到nodejs目录下。
var ws = require("nodejs-websocket");console.log("开始建立连接...")var game1 = null,game2 = null , game1Ready = false , game2Ready = false;var server = ws.createServer(function(conn){ conn.on("text", function (str) { console.log("收到的信息为:"+str) if(str==="game1"){ game1 = conn; game1Ready = true; //conn.sendText("success"); } if(str==="game2"){ game2 = conn; game2Ready = true; //conn.sendText("success"); } if(game1Ready && game2Ready) { if(str==="game1" || str==="game2"){ game1.sendText("success"); game2.sendText("success"); } else{ game1.sendText(str); game2.sendText(str); } } //conn.sendText(str); }) conn.on("close", function (code, reason) { console.log("关闭连接") }); conn.on("error", function (code, reason) { console.log("异常关闭") });}).listen(8001)console.log("WebSocket建立完毕")
然后 创建game1.html和game2.html,为了方便起见也放到nodejs目录下.
game1.html:
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> .kuang{text-align: center;margin-top:200px;} #mess{text-align: center} .value{width: 200px;height:200px;border:1px solid;text-align: center;line-height: 200px;display: inline-block;} </style></head><body><div id="mess">正在连接...</div><div id="content"></div><div> <input type="text" id="mytext"><input type="button" id="dosend" value="发送"></div><script> function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; } var mess = document.getElementById("mess"); var content = document.getElementById("content"); if(window.WebSocket){ var ws = new WebSocket('ws://127.0.0.1:8001'); ws.onopen = function(e){ console.log("连接服务器成功"); ws.send("game1"); } ws.onclose = function(e){ console.log("服务器关闭"); } ws.onerror = function(){ console.log("连接出错"); } ws.onmessage = function(e){ if(e.data==="success"){ mess.innerHTML = "连接成功"; } else{ content.innerHTML+=getNowFormatDate()+"<br>"+e.data+"<br><br>"; } } document.querySelector("#dosend").onclick = function(e){ ws.send("g1:"+document.querySelector("#mytext").value); document.querySelector("#mytext").value = ""; } }</script></body></html>
game2.html:
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> .kuang{text-align: center;margin-top:200px;} #mess{text-align: center} .value{width: 200px;height:200px;border:1px solid;text-align: center;line-height: 200px;display: inline-block;} </style></head><body><div id="mess">正在连接...</div><div id="content"></div><div> <input type="text" id="mytext"><input type="button" id="dosend" value="发送"></div><script> function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; } var mess = document.getElementById("mess"); var content = document.getElementById("content"); if(window.WebSocket){ var ws = new WebSocket('ws://127.0.0.1:8001'); ws.onopen = function(e){ console.log("连接服务器成功"); ws.send("game2"); } ws.onclose = function(e){ console.log("服务器关闭"); } ws.onerror = function(){ console.log("连接出错"); } ws.onmessage = function(e){ if(e.data==="success"){ mess.innerHTML = "连接成功"; } else{ content.innerHTML+=getNowFormatDate()+"<br>"+e.data+"<br><br>"; } } document.querySelector("#dosend").onclick = function(e){ ws.send("g2:"+document.querySelector("#mytext").value); document.querySelector("#mytext").value = ""; } }</script></body></html>
好的 我们主要的文件都已经创建好了
接下来我们在nodejs目录下运行cmd,键入 node ws.js 回车,会有如下提示:
然后我们用chrome分别打开 game1.html和game2.html。最好以两个窗口模式访问,这样更直观。
如图:
请注意截图的两个访问地址,不用通过服务器进行访问就可以。
当两个页面都打开以后,两个页面的顶部会同时显示“”“连接成功”。
接下来任意一个窗口发消息后,两个窗口都会同时受到消息。
0 0
- html5 nodejs&websocket聊天demo
- java+html5的websocket简单实现BS聊天DEMO实例
- html5-websocket聊天例子
- html5 websocket demo
- HTML5 WebSocket DEMO示例
- HTML5 WebSocket + NodeJs 实现聊天室
- HTML5+NodeJs实现WebSocket即时通讯
- HTML5+NodeJs实现WebSocket即时通讯
- HTML5+NodeJs实现WebSocket即时通讯
- html5 websocket java 联合开发---仿qq多人在线聊天demo
- 5分钟搭建一个HTML5视频聊天Demo(WebRTC+NodeJS)
- 5分钟搭建一个HTML5视频聊天Demo(WebRTC+NodeJS)
- 5分钟搭建一个HTML5视频聊天Demo(WebRTC+NodeJS)
- 5分钟搭建一个HTML5视频聊天Demo(WebRTC+NodeJS)
- NodeJs WS 模块 实现 WebSocket 聊天
- 一个基于netty的websocket聊天demo
- WebSocket使用(C++环境)(三) --- 自己解析websocket协议及websocket聊天demo
- 基于Node.js + socket.io实现WebSocket的聊天DEMO
- js 动态拼接已有的变量名并调用
- pyqt5 QMYSQL driver not loaded
- wget
- 有关Python初学者对于yield于生成器(generator)疑惑的终极解答
- Linux下防火墙设置
- html5 nodejs&websocket聊天demo
- 面向对象高级特性
- 浅谈前端集成解决方案
- 界面手势滑动工具类
- poj 1414 dfs 搜索
- python爬取豆瓣电影Top250
- Vijos 1012 清帝之惑之雍正
- 多线程辅助类之数组(二)
- Linux下iscsi的配置与使用