-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetwork.gd
More file actions
69 lines (51 loc) · 1.95 KB
/
Network.gd
File metadata and controls
69 lines (51 loc) · 1.95 KB
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
extends Node
const DEFAULT_PORT = 28960
const MAX_CLIENTS = 6
var server = null
var client = null
var ip_address = ""
var client_connected_to_server = false
onready var client_connection_timeout_timer = Timer.new()
func _ready() -> void:
add_child(client_connection_timeout_timer)
client_connection_timeout_timer.wait_time = 10
client_connection_timeout_timer.one_shot = true
client_connection_timeout_timer.connect("timeout", self, "_client_connection_timeout")
# set IP for hosting by OS type
if OS.get_name() == "Windows":
ip_address = IP.get_local_addresses()[3]
elif OS.get_name() == "Android":
ip_address = IP.get_local_addresses()[0]
else:
ip_address = IP.get_local_addresses()[3]
for ip in IP.get_local_addresses():
if ip.begins_with("192.168.") and not ip.ends_with(".1"):
ip_address = ip
get_tree().connect("connected_to_server", self, "_connected_to_server")
get_tree().connect("server_disconnected", self, "_server_disconnected")
get_tree().connect("connection_failed", self, "_connection_failed")
func create_server() -> void:
server = NetworkedMultiplayerENet.new()
server.create_server(DEFAULT_PORT, MAX_CLIENTS)
get_tree().set_network_peer(server)
func join_server() -> void:
client = NetworkedMultiplayerENet.new()
client.create_client(ip_address, DEFAULT_PORT)
get_tree().set_network_peer(client)
client_connection_timeout_timer.start()
func _connected_to_server() -> void:
print("Successfully connected to the server")
client_connected_to_server = true
func _server_disconnected() -> void:
# Server kicked us; show error and abort.
print("Disconnected from the server")
func _client_connection_timeout():
if client_connected_to_server == false:
print("Client has been timed out")
reset_network_connection()
func _connection_failed():
# Could not even connect to server; abort.
reset_network_connection()
func reset_network_connection() -> void:
if get_tree().has_network_peer():
get_tree().network_peer = null