wsclient.tscn 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. [gd_scene load_steps=3 format=2]
  2. [sub_resource type="GDScript" id=1]
  3. script/source = "extends Control
  4. signal ws_out(msg)
  5. signal ws_login_in(msg)
  6. signal ws_candidate_in(mid, index, sdp)
  7. signal ws_sesssion_in(type, sdp)
  8. # Declare member variables here. Examples:
  9. # var a = 2
  10. # var b = \"text\"
  11. var wsclient = WebSocketClient.new()
  12. var logged_in = true
  13. var username = \"\"
  14. var time_passed = 0
  15. var worldlobby = Control
  16. var game = Control
  17. # Called when the node enters the scene tree for the first time.
  18. func _ready():
  19. wsclient.connect(\"connection_closed\", self, \"on_connection_closed\")
  20. wsclient.connect(\"connection_error\", self, \"on_connection_error\")
  21. wsclient.connect(\"connection_established\", self, \"on_connection_established\")
  22. wsclient.connect(\"connection_failed\", self, \"on_connection_failed\")
  23. wsclient.connect(\"connection_succeeded\", self, \"on_connection_succeeded\")
  24. wsclient.connect(\"data_received\", self, \"on_data_received\")
  25. .connect(\"ws_out\", self, \"send\")
  26. func on_connection_closed(clean):
  27. print(\"connection closed \", clean)
  28. func on_connection_error():
  29. print(\"connection error\")
  30. func on_connection_established(proto):
  31. print(\"connection established\", proto)
  32. func on_connection_failed():
  33. print(\"connection failed\")
  34. func on_connection_succeeded():
  35. print(\"connection succeeded\")
  36. func on_data_received():
  37. var data=wsclient.get_peer(1).get_packet().get_string_from_utf8()
  38. print(\"data received:\", data)
  39. var msg = parse_json(data)
  40. if msg[\"mtype\"] == \"login-succeeded\":
  41. logged_in = true
  42. username = msg[\"payload\"]
  43. $menu/username.text = msg[\"payload\"]
  44. emit_signal(\"ws_login_in\", msg)
  45. elif msg[\"mtype\"] == \"login-needed\":
  46. logged_in = false
  47. elif msg[\"mtype\"] == \"candidate\":
  48. emit_signal(\"ws_candidate_in\", msg[\"payload\"])
  49. elif msg[\"mtype\"] == \"session\":
  50. emit_signal(\"ws_session_in\", msg[\"payload\"])
  51. elif msg[\"mtype\"] == \"world:list\":
  52. worldlobby.getWorlds(msg[\"payload\"])
  53. elif msg[\"mtype\"] == \"game:event\":
  54. game.event(msg[\"payload\"])
  55. elif msg[\"mtype\"] == \"game:get\":
  56. game.getGame(msg[\"payload\"])
  57. $menu/received.text=$menu/received.text+data+\"\\n\"
  58. func send(data):
  59. print(\"sending\", data)
  60. wsclient.get_peer(1).put_packet(to_json(data).to_utf8())
  61. func login():
  62. var data = {\"mtype\": \"login\", \"payload\": str($menu/Connect/Username.text)+\"\\uffff\"+str($menu/Connect/Password.text)}
  63. print(\"sending \", data)
  64. send(data)
  65. # Called every frame. 'delta' is the elapsed time since the previous frame.
  66. func _process(delta):
  67. wsclient.poll()
  68. if wsclient.get_connection_status() == wsclient.CONNECTION_CONNECTED:
  69. $menu/Connect.hide()
  70. $menu/Disconnect.show()
  71. if not logged_in:
  72. if time_passed >= 1:
  73. login()
  74. time_passed=0
  75. else:
  76. time_passed = time_passed + delta
  77. $menu/connstatus.text=wsclient.get_connected_host()
  78. elif wsclient.get_connection_status() == wsclient.CONNECTION_CONNECTING:
  79. $menu/Connect.hide()
  80. $menu/Disconnect.hide()
  81. $menu/connstatus.text=\"connecting...\"
  82. else:
  83. $menu/Connect.show()
  84. $menu/Disconnect.hide()
  85. $menu/connstatus.text=\"not connected\"
  86. func _on_connectButton_pressed():
  87. print(\"connecting to \"+\"ws://rc3.hacknplay.org:5000/ws\")
  88. var err = wsclient.connect_to_url(\"ws://rc3.hacknplay.org:5000/ws\")
  89. if err != 0:
  90. print(\"connection error \"+String(err))
  91. func _on_disconnectButton_pressed():
  92. wsclient.disconnect_from_host()
  93. func _on_sendButton_pressed():
  94. var data = {\"mtype\": \"echo\", \"payload\": \"echo\"}
  95. wsclient.get_peer(1).put_packet(to_json(data).to_utf8())
  96. func setWorldLobby(wl):
  97. worldlobby = wl
  98. func setGame(Game):
  99. game = Game
  100. "
  101. [sub_resource type="StyleBoxFlat" id=2]
  102. [node name="WSClient" type="Control"]
  103. anchor_right = 1.0
  104. anchor_bottom = 1.0
  105. margin_left = -2.3974
  106. margin_top = 3.10672
  107. margin_right = -712.397
  108. margin_bottom = -247.893
  109. script = SubResource( 1 )
  110. __meta__ = {
  111. "_edit_use_anchors_": false
  112. }
  113. [node name="menu" type="Control" parent="."]
  114. margin_left = 274.0
  115. margin_top = 237.0
  116. margin_right = 314.0
  117. margin_bottom = 277.0
  118. __meta__ = {
  119. "_edit_use_anchors_": false
  120. }
  121. [node name="send-Button" type="Button" parent="menu"]
  122. anchor_right = 0.006
  123. margin_left = -175.0
  124. margin_top = -74.0
  125. margin_right = -60.144
  126. margin_bottom = -28.0
  127. text = "echo"
  128. __meta__ = {
  129. "_edit_use_anchors_": false
  130. }
  131. [node name="connstatus" type="Label" parent="menu"]
  132. anchor_left = -6.828
  133. anchor_top = -5.881
  134. anchor_right = -6.828
  135. anchor_bottom = -5.881
  136. margin_left = 38.8798
  137. margin_top = 19.2667
  138. margin_right = 134.88
  139. margin_bottom = 36.2667
  140. text = "not connected"
  141. __meta__ = {
  142. "_edit_use_anchors_": false
  143. }
  144. [node name="exit" type="Button" parent="menu"]
  145. margin_top = -66.8752
  146. margin_right = 30.0
  147. margin_bottom = -32.8752
  148. text = "X"
  149. __meta__ = {
  150. "_edit_use_anchors_": false
  151. }
  152. [node name="received" type="Label" parent="menu"]
  153. margin_left = -177.287
  154. margin_top = -13.5705
  155. margin_right = -57.2869
  156. margin_bottom = 33.4295
  157. custom_styles/normal = SubResource( 2 )
  158. __meta__ = {
  159. "_edit_use_anchors_": false
  160. }
  161. [node name="username" type="Label" parent="menu"]
  162. margin_left = -127.673
  163. margin_top = -214.973
  164. margin_right = -11.6735
  165. margin_bottom = -199.973
  166. text = "not logged in"
  167. __meta__ = {
  168. "_edit_use_anchors_": false
  169. }
  170. [node name="Connect" type="Control" parent="menu"]
  171. margin_right = 40.0
  172. margin_bottom = 40.0
  173. __meta__ = {
  174. "_edit_use_anchors_": false
  175. }
  176. [node name="Username" type="LineEdit" parent="menu/Connect"]
  177. margin_left = -234.786
  178. margin_top = -196.433
  179. margin_right = -128.786
  180. margin_bottom = -170.433
  181. text = "Username"
  182. clear_button_enabled = true
  183. __meta__ = {
  184. "_edit_use_anchors_": false
  185. }
  186. [node name="Password" type="LineEdit" parent="menu/Connect"]
  187. margin_left = -236.327
  188. margin_top = -168.766
  189. margin_right = -130.327
  190. margin_bottom = -142.766
  191. text = "Password"
  192. secret = true
  193. clear_button_enabled = true
  194. __meta__ = {
  195. "_edit_use_anchors_": false
  196. }
  197. [node name="connect-Button" type="Button" parent="menu/Connect"]
  198. margin_left = -126.711
  199. margin_top = -190.367
  200. margin_right = -23.711
  201. margin_bottom = -149.367
  202. text = "connect"
  203. __meta__ = {
  204. "_edit_use_anchors_": false
  205. }
  206. [node name="Disconnect" type="Control" parent="menu"]
  207. margin_right = 40.0
  208. margin_bottom = 40.0
  209. [node name="disconnect-Button" type="Button" parent="menu/Disconnect"]
  210. anchor_right = 0.012
  211. anchor_bottom = 0.017
  212. margin_left = -184.168
  213. margin_top = -188.46
  214. margin_right = -73.4561
  215. margin_bottom = -149.66
  216. text = "disconnect"
  217. __meta__ = {
  218. "_edit_use_anchors_": false
  219. }
  220. [connection signal="pressed" from="menu/send-Button" to="." method="_on_sendButton_pressed"]
  221. [connection signal="pressed" from="menu/Connect/connect-Button" to="." method="_on_connectButton_pressed"]
  222. [connection signal="pressed" from="menu/Disconnect/disconnect-Button" to="." method="_on_disconnectButton_pressed"]