Skip to content

Commit 68b7a47

Browse files
committed
v1.0
1 parent 00552b7 commit 68b7a47

File tree

5 files changed

+462
-0
lines changed

5 files changed

+462
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013-2016 Yuxi Evan You
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

example.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
var port = 11001; //服务器端口
3+
var server = "10.3.0.175"; //服务器IP
4+
var tid = "1"; //客户端或教室ID
5+
6+
var TAG_SEND_REGIST = 1; //客户端注册
7+
var TAG_SEND_PING = 112; //客户端心跳响应
8+
var TAG_FROM_REGIST_SUCCESS = 1; //客户端注册成功
9+
var TAG_FROM_REGIST_FAIL = 0; //客户端注册失败
10+
var TAG_FROM_PING = 112; //服务端心跳指令
11+
var TAG_FROM_END_QUIZ = 8; //答题结束
12+
var TAG_FROM_START_QUIZ = 7; //开始答题
13+
var TAG_FROM_ANSWER = 10; //学生答题
14+
var TAG_FROM_CLOSE = 11; //关闭或未就绪状态
15+
16+
var showDebugData = false;
17+
var needReconnect = true;
18+
19+
//和服务器建立socket连接
20+
var socket = require("./index");
21+
// socket.setDebuger(appendLog);
22+
socket.connect(port, server, function(){
23+
socket.send(TAG_SEND_REGIST, {classId: tid});
24+
}, render, function(){
25+
if (needReconnect) {
26+
setTimeout(socket.connect, 1000);
27+
}
28+
});
29+
30+
function render(tag, value){
31+
if (tag != TAG_FROM_PING)
32+
appendLog('[Received data] tag: ' + tag + ' value: ' + JSON.stringify(value));
33+
34+
switch (tag){
35+
case TAG_FROM_REGIST_SUCCESS: //注册成功
36+
//needReconnect = false;
37+
break;
38+
case TAG_FROM_REGIST_FAIL: //注册失败
39+
//needReconnect = true;
40+
break;
41+
case TAG_FROM_PING: //心跳
42+
socket.send(TAG_SEND_PING);
43+
break;
44+
case TAG_FROM_CLOSE: //关闭
45+
46+
break;
47+
case TAG_FROM_END_QUIZ: //结束答题
48+
49+
break;
50+
case TAG_FROM_START_QUIZ: //开始答题
51+
52+
break;
53+
case TAG_FROM_ANSWER: //学生答题
54+
55+
break;
56+
}
57+
}
58+
59+
function appendLog(log){
60+
if (showDebugData == false) return;
61+
62+
var debugText = document.getElementById('debugText');
63+
if (debugText) {
64+
debugText.innerHTML += log + "<br>";
65+
debugText.scrollTop = debugText.scrollHeight;
66+
}
67+
}

index.js

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
var net = require("net");
2+
var ExBuffer = require("./lib/ExBuffer");
3+
4+
var client = null;
5+
var exBuffer = null;
6+
var connectCallback = null;
7+
var receiveCallback = null;
8+
var closeCallback = null;
9+
var serverPort = null;
10+
var serverHost = null;
11+
var hasShutdown = false;
12+
var debuger = console.log;
13+
14+
function connect(port, host, onConnected, onReceived, onClosed){
15+
close(false);
16+
17+
if (port){
18+
serverPort = port;
19+
}
20+
if (host){
21+
serverHost = host;
22+
}
23+
if (onConnected){
24+
connectCallback = onConnected;
25+
}
26+
if (onReceived){
27+
receiveCallback = onReceived;
28+
}
29+
if (onClosed){
30+
closeCallback = onClosed;
31+
}
32+
33+
client = net.connect(serverPort, serverHost, function(){
34+
debuger('Client connected: ' + serverHost + ":" + serverPort);
35+
36+
if (typeof connectCallback === "function"){
37+
connectCallback();
38+
}
39+
40+
exBuffer = new ExBuffer().int8Tag().uint32Head().bigEndian();
41+
exBuffer.on('data', onReceivePackData);
42+
43+
client.on('data', function(data) {
44+
exBuffer.put(data);
45+
});
46+
47+
client.on('end', function() {
48+
debuger('Client disconnected from server');
49+
if (typeof closeCallback === "function"){
50+
closeCallback();
51+
}
52+
});
53+
54+
client.on('timeout', function() {
55+
debuger('The socket times out.');
56+
//setTimeout(reconnect, 1000);
57+
});
58+
});
59+
60+
client.on('error', function(error){
61+
debuger("The socket had an error: " + error.code);
62+
setTimeout(reconnect, 1000);
63+
});
64+
65+
client.on('close', function() {
66+
debuger('The socket closed.');
67+
});
68+
69+
//client.setTimeout(15000);
70+
}
71+
72+
function reconnect(){
73+
if (!hasShutdown){
74+
connect();
75+
}
76+
}
77+
78+
function close(shutdown = true) {
79+
hasShutdown = shutdown;
80+
if (client){
81+
client.end();
82+
client.destroy();
83+
client = null;
84+
}
85+
}
86+
87+
function send(cmd, arg){
88+
debuger('[Send] tag: ' + cmd + ' value: ' + (arg ? JSON.stringify(arg) : ''));
89+
if (client){
90+
var data = null, len = 0;
91+
if (arg){
92+
data = JSON.stringify(arg);
93+
len = Buffer.byteLength(data);
94+
}
95+
96+
//写入1个字节的tag
97+
var tagBuf = new Buffer(1);
98+
tagBuf.writeInt8(cmd, 0);
99+
client.write(tagBuf);
100+
101+
//写入4个字节的length
102+
var headBuf = new Buffer(4);
103+
headBuf.writeUInt32BE(len, 0);
104+
client.write(headBuf);
105+
106+
//写入value
107+
if (len > 0){
108+
var bodyBuf = new Buffer(len);
109+
bodyBuf.write(data);
110+
client.write(bodyBuf);
111+
}
112+
} else {
113+
debuger("socket 未连接");
114+
}
115+
}
116+
117+
function onReceivePackData(data){
118+
var tag = data.tag;
119+
var value = data.value ? data.value.toString() : null;
120+
debuger('[Received] tag: ' + tag + ' value: ' + value);
121+
122+
if (value){
123+
value = JSON.parse(value);
124+
}
125+
126+
if (typeof receiveCallback === "function"){
127+
receiveCallback(tag, value);
128+
}
129+
}
130+
131+
function setDebuger(value){
132+
debuger = value;
133+
}
134+
135+
module.exports.setDebuger = setDebuger;
136+
module.exports.connect = connect;
137+
module.exports.close = close;
138+
module.exports.send = send;

0 commit comments

Comments
 (0)