first commit
This commit is contained in:
commit
095da02edc
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
dist
|
||||
98
app.js
Normal file
98
app.js
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
TODO:
|
||||
* 在指定的房间内广播数据 BroadcastInRoom(roomid, msg)
|
||||
* 探索出能解决将同一roomid的客户加入房间的问题的方法
|
||||
FIXME:
|
||||
* 暂无待修正
|
||||
XXX:
|
||||
* 房间功能
|
||||
*/
|
||||
|
||||
|
||||
// 导入模块
|
||||
var WebsocketServer = require('websocket').server;
|
||||
var http = require('http');
|
||||
//const { client } = require('websocket');
|
||||
|
||||
// 创建http服务器(用于承载WS)
|
||||
var server = http.createServer();
|
||||
// 端口设置端口
|
||||
const PORT = 3000 || process.env.PORT
|
||||
// 客户端列表
|
||||
clientsList = [];
|
||||
|
||||
// http绑定端口
|
||||
server.listen(PORT, () => {
|
||||
console.log("Server running on http://localhost:" + PORT);
|
||||
});
|
||||
|
||||
// 在http服务器上运行WS服务器
|
||||
var wsServer = new WebsocketServer({httpServer:server});
|
||||
|
||||
// 当客户端连入时
|
||||
wsServer.on('request', (websocketRequest) => {
|
||||
var connection = websocketRequest.accept(null, 'accepted-origin');
|
||||
//将客户端插入终端列表
|
||||
clientsList.push(connection);
|
||||
console.log('A client connected');
|
||||
|
||||
// 当收到消息时
|
||||
connection.on('message', (msg) => {
|
||||
// 判断消息类型
|
||||
if(msg.type == 'utf8'){
|
||||
// 过滤非法数据
|
||||
try {
|
||||
/*
|
||||
定义的数据传送格式规范(JSON):
|
||||
tag:数据标签
|
||||
msg:数据内容
|
||||
opt:Boolean,确认是否为执行操作,操作tag与用户传输的tag重合。
|
||||
args: opt参数
|
||||
*/
|
||||
// 解析数据
|
||||
cd = JSON.parse(msg.utf8Data);
|
||||
|
||||
// 开发中,将客户端加入某个房间
|
||||
if(cd.tag == 'setRoom' && cd.opt == true){
|
||||
// 如果客户端对象没有设置过roomid属性则增设
|
||||
if(connection.roomid == undefined){
|
||||
connection.roomid = [];
|
||||
}
|
||||
// 将传进来的roomid属性写入
|
||||
connection.roomid.push(cd.msg);
|
||||
console.log("Set! roomid = " + connection.roomid);
|
||||
}
|
||||
|
||||
if(cd.tag == 'roomcast' && cd.opt == true && cd.arg != undefined){
|
||||
console.log('Cast!')
|
||||
BroadcastInRoom(cd.arg.roomid, 'cast', cd.msg);
|
||||
}else if(cd.opt == true && cd.arg == undefined){
|
||||
console.log('Cannot GET arg');
|
||||
}else{
|
||||
console.log('Unknown ERROR');
|
||||
}
|
||||
|
||||
console.log(cd);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
console.error('Illegal Data');
|
||||
}
|
||||
}else{
|
||||
console.log(msg);
|
||||
}
|
||||
});
|
||||
|
||||
connection.on('close', (reasonCode, description) => {
|
||||
console.log('A client disconnected');
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
// 房间内消息广播
|
||||
function BroadcastInRoom(roomid, tag, msg){
|
||||
for(var i = 0; i < clientsList.length; i++){
|
||||
if(clientsList[i].roomid == roomid){
|
||||
clientsList[i].send(JSON.stringify({'tag': tag, 'msg': msg}));
|
||||
}
|
||||
}
|
||||
}
|
||||
136
package-lock.json
generated
Normal file
136
package-lock.json
generated
Normal file
@ -0,0 +1,136 @@
|
||||
{
|
||||
"name": "wstest",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"bufferutil": {
|
||||
"version": "4.0.6",
|
||||
"resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.6.tgz",
|
||||
"integrity": "sha512-jduaYOYtnio4aIAyc6UbvPCVcgq7nYpVnucyxr6eCYg/Woad9Hf/oxxBRDnGGjPfjUm6j5O/uBWhIu4iLebFaw==",
|
||||
"requires": {
|
||||
"node-gyp-build": "^4.3.0"
|
||||
}
|
||||
},
|
||||
"d": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz",
|
||||
"integrity": "sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==",
|
||||
"requires": {
|
||||
"es5-ext": "^0.10.50",
|
||||
"type": "^1.0.1"
|
||||
}
|
||||
},
|
||||
"debug": {
|
||||
"version": "2.6.9",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
|
||||
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
},
|
||||
"es5-ext": {
|
||||
"version": "0.10.61",
|
||||
"resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.61.tgz",
|
||||
"integrity": "sha512-yFhIqQAzu2Ca2I4SE2Au3rxVfmohU9Y7wqGR+s7+H7krk26NXhIRAZDgqd6xqjCEFUomDEA3/Bo/7fKmIkW1kA==",
|
||||
"requires": {
|
||||
"es6-iterator": "^2.0.3",
|
||||
"es6-symbol": "^3.1.3",
|
||||
"next-tick": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"es6-iterator": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz",
|
||||
"integrity": "sha512-zw4SRzoUkd+cl+ZoE15A9o1oQd920Bb0iOJMQkQhl3jNc03YqVjAhG7scf9C5KWRU/R13Orf588uCC6525o02g==",
|
||||
"requires": {
|
||||
"d": "1",
|
||||
"es5-ext": "^0.10.35",
|
||||
"es6-symbol": "^3.1.1"
|
||||
}
|
||||
},
|
||||
"es6-symbol": {
|
||||
"version": "3.1.3",
|
||||
"resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.3.tgz",
|
||||
"integrity": "sha512-NJ6Yn3FuDinBaBRWl/q5X/s4koRHBrgKAu+yGI6JCBeiu3qrcbJhwT2GeR/EXVfylRk8dpQVJoLEFhK+Mu31NA==",
|
||||
"requires": {
|
||||
"d": "^1.0.1",
|
||||
"ext": "^1.1.2"
|
||||
}
|
||||
},
|
||||
"ext": {
|
||||
"version": "1.6.0",
|
||||
"resolved": "https://registry.npmjs.org/ext/-/ext-1.6.0.tgz",
|
||||
"integrity": "sha512-sdBImtzkq2HpkdRLtlLWDa6w4DX22ijZLKx8BMPUuKe1c5lbN6xwQDQCxSfxBQnHZ13ls/FH0MQZx/q/gr6FQg==",
|
||||
"requires": {
|
||||
"type": "^2.5.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"type": {
|
||||
"version": "2.6.0",
|
||||
"resolved": "https://registry.npmjs.org/type/-/type-2.6.0.tgz",
|
||||
"integrity": "sha512-eiDBDOmkih5pMbo9OqsqPRGMljLodLcwd5XD5JbtNB0o89xZAwynY9EdCDsJU7LtcVCClu9DvM7/0Ep1hYX3EQ=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"is-typedarray": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
|
||||
"integrity": "sha512-cyA56iCMHAh5CdzjJIa4aohJyeO1YbwLi3Jc35MmRU6poroFjIGZzUzupGiRPOjgHg9TLu43xbpwXk523fMxKA=="
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
|
||||
},
|
||||
"next-tick": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.1.0.tgz",
|
||||
"integrity": "sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ=="
|
||||
},
|
||||
"node-gyp-build": {
|
||||
"version": "4.5.0",
|
||||
"resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.5.0.tgz",
|
||||
"integrity": "sha512-2iGbaQBV+ITgCz76ZEjmhUKAKVf7xfY1sRl4UiKQspfZMH2h06SyhNsnSVy50cwkFQDGLyif6m/6uFXHkOZ6rg=="
|
||||
},
|
||||
"type": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz",
|
||||
"integrity": "sha512-+5nt5AAniqsCnu2cEQQdpzCAh33kVx8n0VoFidKpB1dVVLAN/F+bgVOqOJqOnEnrhp222clB5p3vUlD+1QAnfg=="
|
||||
},
|
||||
"typedarray-to-buffer": {
|
||||
"version": "3.1.5",
|
||||
"resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz",
|
||||
"integrity": "sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==",
|
||||
"requires": {
|
||||
"is-typedarray": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"version": "5.0.9",
|
||||
"resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-5.0.9.tgz",
|
||||
"integrity": "sha512-Yek7dAy0v3Kl0orwMlvi7TPtiCNrdfHNd7Gcc/pLq4BLXqfAmd0J7OWMizUQnTTJsyjKn02mU7anqwfmUP4J8Q==",
|
||||
"requires": {
|
||||
"node-gyp-build": "^4.3.0"
|
||||
}
|
||||
},
|
||||
"websocket": {
|
||||
"version": "1.0.34",
|
||||
"resolved": "https://registry.npmjs.org/websocket/-/websocket-1.0.34.tgz",
|
||||
"integrity": "sha512-PRDso2sGwF6kM75QykIesBijKSVceR6jL2G8NGYyq2XrItNC2P5/qL5XeR056GhA+Ly7JMFvJb9I312mJfmqnQ==",
|
||||
"requires": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"debug": "^2.2.0",
|
||||
"es5-ext": "^0.10.50",
|
||||
"typedarray-to-buffer": "^3.1.5",
|
||||
"utf-8-validate": "^5.0.2",
|
||||
"yaeti": "^0.0.6"
|
||||
}
|
||||
},
|
||||
"yaeti": {
|
||||
"version": "0.0.6",
|
||||
"resolved": "https://registry.npmjs.org/yaeti/-/yaeti-0.0.6.tgz",
|
||||
"integrity": "sha512-MvQa//+KcZCUkBTIC9blM+CU9J2GzuTytsOUwf2lidtvkx/6gnEp1QvJv34t9vdjhFmha/mUiNDbN0D0mJWdug=="
|
||||
}
|
||||
}
|
||||
}
|
||||
14
package.json
Normal file
14
package.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "wstest",
|
||||
"version": "1.0.0",
|
||||
"description": "None",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "star",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"websocket": "^1.0.34"
|
||||
}
|
||||
}
|
||||
52
web/index.html
Normal file
52
web/index.html
Normal file
@ -0,0 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Socket</title>
|
||||
</head>
|
||||
<body onload="InitWS()">
|
||||
<div><input type="text" id="textin"><button onclick="send()">Submit</button> <button onclick="Broadcast()">Submit to room</button></div>
|
||||
<input type="text" id="roomid"><button onclick="joinTheRoom()">JOIN</button>
|
||||
<script>
|
||||
aROOM = 0
|
||||
function joinTheRoom(){
|
||||
aROOM = document.getElementById('roomid').value;
|
||||
var Datas = {'tag': 'setRoom', 'msg': aROOM, 'opt': true};
|
||||
ws.send(JSON.stringify(Datas));
|
||||
// 结果返回
|
||||
}
|
||||
|
||||
function send(){
|
||||
var context = document.getElementById('textin').value;
|
||||
var data = {'tag': 'roomcast', 'msg': context, 'opt': false};
|
||||
ws.send(JSON.stringify(data));
|
||||
}
|
||||
|
||||
function Broadcast(){
|
||||
var context = document.getElementById('textin').value;
|
||||
var data = {'tag': 'roomcast', 'msg': context, 'opt': true, 'arg': aROOM};
|
||||
ws.send(JSON.stringify(data));
|
||||
}
|
||||
|
||||
function InitWS(){
|
||||
if("WebSocket" in window){
|
||||
console.log("您的浏览器支持Websocket");
|
||||
ws = new WebSocket("ws://localhost:3000");
|
||||
ws.onopen = function(){
|
||||
console.log("connected!");
|
||||
}
|
||||
|
||||
ws.onmessage = function(evt){
|
||||
var rece = evt.data;
|
||||
console.log("received: "+ rece);
|
||||
}
|
||||
|
||||
ws.onclose = function(){
|
||||
console.log("disconnected!");
|
||||
}
|
||||
}else{
|
||||
alert("您的浏览器不支持Websocket");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user