-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (78 loc) · 2.19 KB
/
index.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module.exports=function(onmessage,port){
var ws = require("nodejs-websocket");
var child_process = require('child_process');
global.fs = require("fs")
console.log("Started to Create...")
Date.prototype.format = function(fmt)
{
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
"H+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
return ws.createServer(function(conn){
conn.sendcommand=function(execstr){
conn.sendText(`{
"body": {
"origin": {
"type": "player"
},
"commandLine": "`+execstr+`",
"version": 1
},
"header": {
"requestId": "00000000-0000-0000-0000-000000000000",
"messagePurpose": "commandRequest",
"version": 1,
"messageType": "commandRequest"
}
}`);
}
onmessage("create",`{
"body": {
"eventName": "socketcreated",
,
"properties": {
"text":"A User Connect You!"
}
}
}`,conn);
setTimeout(function(){
var eventlist=["BossKilled","BlockBroken","BlockPlaced","ItemUsed","CraftingSessionCompleted","PlayerTravelled","ScreenChanged","MobKilled","EntitySpawned","PlayerMessage","CommandResponse"];
for (var ss=0;ss<eventlist.length;ss++)
conn.sendText(`{
"body": {
"eventName": "`+eventlist[ss]+`"
},
"header": {
"requestId": "00000000-0000-0000-0000-000000000000",
"messagePurpose": "subscribe",
"version": 1,
"messageType": "commandRequest"
}
}`);
},5000);
conn.on("text", function (str) {
onmessage("text",JSON.parse(str),conn);
})
conn.on("close", function (code, reason) {
console.log("关闭连接",+code+","+reason)
});
conn.on("error", function (code, reason) {
console.log("异常关闭")
});
}).listen(port)
console.log("WebSocket done")
}