-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketNode.gd
195 lines (163 loc) · 5.26 KB
/
SocketNode.gd
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
extends Node
const PomeloMessageClass = preload('res://PomeloMessage.gd')
var pomelo = PomeloMessageClass.new()
var socket = StreamPeerTCP.new()
var _accum = 0 # delta accumulator
var _active = false # socket intended for use
var _connected = false # socket is connected
var nextHeartbeatTimeout = 0
var needSendHeartBeat = true
var curTime = 0
const ERR = 1
export(String) var host = "127.0.0.1"
export(int) var port = 3014
var handlerFuncs = {}
var requestId = 1
signal login(name, roomid)
signal enterServer(name, roomid)
signal say(content)
var userid = ""
var roomid = 1
var clientState = ""
func _ready():
connect_to_server(host, port)
set_process(true)
connect("login", self, "loginWithNameAndRoomId")
connect("enterServer", self, "getServerInfo")
connect("say", self, "sendChat")
func _process(delta):
curTime = curTime + delta
_readloop(delta)
func write(string):
if(_connected):
socket.put_utf8_string(string)
func connect_to_server(host, port):
_active = true
var err = socket.connect_to_host(host, port)
_connected = socket.is_connected_to_host()
if _connected:
#socket.put_8(1)
needSendHeartBeat = true
pomelo.handshakeFirst(socket)
pass
return err
func disconnect():
_active = false
_connected = false
needSendHeartBeat = false
socket.disconnect_from_host()
func _readloop(delta):
if(not _active):
return
pass
# TODO, emit errors and data
_accum += delta
if(_accum > 1):
_accum = 0
var connected = socket.is_connected_to_host()
if(not connected):
print("Lost Connection")
_respond("Lost Connection", ERR)
else:
var output = socket.get_partial_data(socket.get_available_bytes())
var errCode = output[0]
var outputData = output[1]
if(errCode != 0):
_respond( "ErrCode:" + str(errCode), ERR)
else:
var outStr = outputData.get_string_from_utf8()
if(outStr != ""):
_respond( outputData, 0 )
if curTime >= nextHeartbeatTimeout and needSendHeartBeat == true:
pomelo.heartBeat(socket)
needSendHeartBeat = false
func _respond(outputData, errCode):
if(errCode == 0):
_respondOK(outputData)
elif(errCode == ERR):
_respondErr(outputData)
func _respondOK(outputData):
# TODO emit?
var msgs = pomelo.processPackage(socket, outputData)
if typeof(msgs) == TYPE_ARRAY:
for i in range(msgs.size()):
handlerWithType(socket, msgs[i].type, msgs[i].body)
pass
else:
handlerWithType(socket, msgs.type, msgs.body)
pass
func handlerWithType(socket, type, body):
if type == pomelo.TYPE_HANDSHAKE:
print(PoolByteArray(body).get_string_from_utf8())
pomelo.handshakeACK(socket)
if clientState == "wait_to_login":
emit_signal("login", userid, roomid)
clientState = ""
pass
if type == pomelo.TYPE_HEARTBEAT:
print("get heart beat")
needSendHeartBeat = true
nextHeartbeatTimeout = curTime + 3
if type == pomelo.TYPE_DATA:
var resoult = pomelo.decodeMessage(body)
if resoult.id > 0 && resoult.type == pomelo.TYPE_RESPONSE && handlerFuncs[resoult.id] == "gate.gateHandler.queryEntry":
disconnect()
var jsonstring = PoolByteArray(resoult.body).get_string_from_utf8()
var jsonresult = JSON.parse(jsonstring)
connect_to_server(jsonresult.result.host, jsonresult.result.port)
clientState = "wait_to_login"
if resoult.id > 0 && resoult.type == pomelo.TYPE_RESPONSE && handlerFuncs[resoult.id] == "connector.entryHandler.enter":
var jsonstring = PoolByteArray(resoult.body).get_string_from_utf8()
var jsonresult = JSON.parse(jsonstring)
$roomRoot.firstShowUsers(jsonresult.result.users)
$roomRoot.visible = true
pass
if resoult.type == pomelo.TYPE_PUSH:
if resoult.route == "onChat":
var jsonstring = PoolByteArray(resoult.body).get_string_from_utf8()
var jsonresult = JSON.parse(jsonstring)
var format_string = "%s:%s"
var text = format_string % [jsonresult.result.from, jsonresult.result.msg]
$roomRoot.addMsgToList(text)
if resoult.route == "onAdd":
var jsonstring = PoolByteArray(resoult.body).get_string_from_utf8()
var jsonresult = JSON.parse(jsonstring)
$roomRoot.addUser(jsonresult.result.user)
if resoult.route == "onLeave":
var jsonstring = PoolByteArray(resoult.body).get_string_from_utf8()
var jsonresult = JSON.parse(jsonstring)
$roomRoot.removeUser(jsonresult.result.user)
func _respondErr(outputData):
# TODO emit?
print(outputData)
pass
func getServerInfo(name, roomid):
userid = name
roomid = roomid
if(_connected):
requestId = requestId + 1
pomelo.sendMessage(socket, requestId, "gate.gateHandler.queryEntry", to_json({"uid":name}))
handlerFuncs[requestId] = "gate.gateHandler.queryEntry"
print(handlerFuncs)
pass # replace with function body
func loginWithNameAndRoomId(name, roomid):
if(_connected):
requestId = requestId + 1
pomelo.sendMessage(socket, requestId, "connector.entryHandler.enter", to_json({"username":name, "rid":roomid }))
handlerFuncs[requestId] = "connector.entryHandler.enter"
print(handlerFuncs)
pass # replace with function body
func sendChat(content):
if _connected:
print("send:")
print(content)
requestId = requestId + 1
var msg = {
"rid": userid,
"content": content,
"from": userid,
"target": "*"
}
pomelo.sendMessage(socket, requestId, "chat.chatHandler.send", to_json(msg))
handlerFuncs[requestId] = "chat.chatHandler.send"
pass