forked from you-win/http-util-gd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.gd
82 lines (61 loc) · 2.26 KB
/
demo.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
extends CanvasLayer
const HttpUtil = preload("res://addons/http-util/http_util.gd")
var _server: HttpUtil.Server
#-----------------------------------------------------------------------------#
# Builtin functions #
#-----------------------------------------------------------------------------#
func _init() -> void:
OS.center_window()
_server = HttpUtil.Server.new({
"routes": {
"/": {
"handler": self,
"method": "default",
"args": [
"some arg"
]
},
"/declarative": {
"handler": self,
"method": "hello_declarative",
"options": {
"pass_stream_peer": true,
"can_return_null": true
}
}
},
"options": {
}
})
_server.add_route("/imperative").handler(self).method("imperative_hello").build()
_server.start(9999)
func _input(event: InputEvent) -> void:
if not event is InputEventMouseButton:
return
event = event as InputEventMouseButton
if event.pressed and event.button_index == BUTTON_LEFT:
print("is_alive %s" % str(_server._server_thread.is_alive()))
print("is_active %s" % str(_server._server_thread.is_active()))
func _exit_tree() -> void:
_server.stop()
#-----------------------------------------------------------------------------#
# Connections #
#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
# Private functions #
#-----------------------------------------------------------------------------#
func default(response, something) -> int:
print("index")
print("my arg %s" % str(something))
return OK
func hello_declarative(response, peer):
print("hello declarative")
print(peer.get_status())
(peer as StreamPeerTCP).disconnect_from_host()
func imperative_hello(response: Dictionary) -> int:
print("Hello imperative")
response.body = "hello"
return OK
#-----------------------------------------------------------------------------#
# Public functions #
#-----------------------------------------------------------------------------#