-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple_Cloud.js
224 lines (208 loc) · 7.07 KB
/
Simple_Cloud.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
(function (Scratch) {
'use strict';
let Server;
class SimpleCloud {
constructor() {
this.isRunning = false
this.messageNum = 0;
this.username = null;
this.Message_List='';
this.GlobalVar='';
}
getInfo() {
return {
id: 'Q3dlaXpoaQ',
name: '简单联机',
color1: '#8608fc',
color3:'#FFFFFF',
blocks: [{
opcode: 'Connect',
blockType: 'command',
text: '连接到[IP]',
arguments: {
IP: {
type: 'string',
defaultValue: 'wss://clvtpd-qnmsta-1206.preview.myide.io'
}
}
},
{
opcode: 'SetID',
blockType: 'command',
text: '将用户名设置为[ID]',
arguments: {
ID: {
type: 'string',
defaultValue: ''
}
}
},
{
opcode: 'CloseSocket',
blockType: 'command',
text: '断开连接'
},
{
opcode: 'SendMessage',
blockType: 'command',
text: '发送[MESSAGE]到服务器',
arguments: {
MESSAGE: {
type: 'string',
defaultValue: ''
}
}
},
{
opcode: 'SetGlobalVar',
blockType: 'command',
text: '将全局变量设为[Var]',
arguments: {
Var: {
type: 'string',
defaultValue: ''
}
}
},
{
opcode: 'Return_Server_List',
blockType: 'reporter',
text: '服务器列表',
},
{
opcode: 'Return_username',
blockType: 'reporter',
text: '用户名',
},
{
opcode: 'Return_receive',
blockType: 'reporter',
text: '接收到的信息',
},
{
opcode: 'Return_GVar',
blockType: 'reporter',
text: '全局云变量',
},
{
opcode: 'Return_message_Num',
blockType: 'reporter',
text: '共收到的信息量',
},
{
opcode: 'Return_Auther',
blockType: 'reporter',
text: '作者',
},
{
opcode: 'Return_isConnect',
blockType: 'Boolean',
text: '是否连接到服务器?',
},
],
};
}
async Connect({
IP
}) {
const self = this;
if (!self.isRunning) {
Server = new WebSocket(String(IP))
Server.onopen = function () {
self.isRunning = true
};
Server.onmessage = function (str) {
let temp_json = JSON.parse(str.data)
if (temp_json instanceof Array) {
self.Message_List = JSON.stringify(temp_json)
self.messageNum++
}
else if (temp_json instanceof Object) {
delete temp_json['cmd']
self.GlobalVar = JSON.stringify(temp_json['var'])
}
}
}
}
SendMessage({
MESSAGE
}) {
if (this.username != null) {
let tempjson = {
'name': this.username,
'val': {}
}
tempjson['val'] = JSON.parse(MESSAGE)
Server.send(JSON.stringify(tempjson))
}
}
SetGlobalVar({
Var
}) {
if (this.username != null) {
let temp_json = {
'cmd': 'GVar',
'var': JSON.parse(Var)
}
Server.send(JSON.stringify(temp_json))
}
}
SetID({
ID
}) {
this.username = String(ID)
}
Return_receive() {
return String(this.Message_List);
}
Return_isConnect() {
const self = this
if (Server==undefined){
self.isRunning=false
}
else if (Server.readyState == 1) {
self.isRunning = true
}
else {
self.isRunning = false
self.GlobalVar=''
self.Message_List=''
self.username = null
}
return String(self.isRunning)
}
Return_message_Num() {
return String(this.messageNum);
}
Return_username() {
return String(this.username);
}
Return_GVar() {
return String(this.GlobalVar);
}
Return_Server_List() {
return "{wss://clvtpd-qnmsta-1206.preview.myide.io,\
wss://b4b72ea4-1206-app.lightly.teamcode.com?port=1206&dcsId=b4b72ea4&token=B1zgKRFqRIW7N5ZL7FvZJA}"
}
Return_Auther(){
return "@Q3dlaXpoaQ"
}
CloseSocket() {
const self = this;
if (this.isRunning) {
if (this.username != null) {
Server.send(JSON.stringify({
'name': this.username,
'val': 'disconnect'
}))
}
this.username = null;
Server.close();
self.isRunning = false;
this.Message_List = ''
this.GlobalVar=''
}
}
}
Scratch.extensions.register(new SimpleCloud());
})(Scratch);