-
Notifications
You must be signed in to change notification settings - Fork 0
/
GClient.gd
68 lines (51 loc) · 1.72 KB
/
GClient.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
extends Node
class_name GClient
var client : StreamPeerTCP
var was_connected: bool
signal on_update_connection_status(connection_status: int)
func initialize():
print("godouse client initialized")
client = StreamPeerTCP.new()
func try_connect_client(host: String, port: int) -> String:
if client.get_status() == StreamPeerTCP.STATUS_CONNECTED:
return "Already connected to %s:%d" % [host, port]
on_update_connection_status.emit(StreamPeerTCP.STATUS_CONNECTING)
var err = client.connect_to_host(host,port)
var timeout = get_tree().create_timer(3)
while timeout.time_left > 0:
if client.get_status() == StreamPeerTCP.STATUS_CONNECTED:
# all ok
GConfig.host = host
GConfig.port = port
on_update_connection_status.emit(StreamPeerTCP.STATUS_CONNECTED)
was_connected = true
return ""
await get_tree().process_frame
# timeout
client.disconnect_from_host()
on_update_connection_status.emit(StreamPeerTCP.STATUS_ERROR)
return "error connecting client\n%s:%d\nerror code: %d" % [host,port,err]
func _process(_delta):
if client == null:
return
client.poll()
if was_connected && client.get_status() != StreamPeerTCP.STATUS_CONNECTED:
on_update_connection_status.emit(StreamPeerTCP.STATUS_ERROR)
client.disconnect_from_host()
was_connected = false
func send_cursor_movement(velocity: Vector2, relative: Vector2):
client.put_var({
type = GConstants.TCP_MESSAGE_TYPE_MOVE_CURSOR,
relative = relative,
velocity = velocity
})
func send_press_mouse_button(button: int):
client.put_var({
type = GConstants.TCP_MESSAGE_TYPE_PRESS_CURSOR_BUTTON,
button = button
})
func send_press_virtual_key(button: int):
client.put_var({
type = GConstants.TCP_MESSAGE_TYPE_VIRTUAL_KEY,
button = button
})