multiplayer_client.gd 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. extends "ws_webrtc_client.gd"
  2. var rtc_mp: WebRTCMultiplayerPeer = WebRTCMultiplayerPeer.new()
  3. var sealed := false
  4. func _init():
  5. connected.connect(self._connected)
  6. disconnected.connect(self._disconnected)
  7. offer_received.connect(self._offer_received)
  8. answer_received.connect(self._answer_received)
  9. candidate_received.connect(self._candidate_received)
  10. lobby_joined.connect(self._lobby_joined)
  11. lobby_sealed.connect(self._lobby_sealed)
  12. peer_connected.connect(self._peer_connected)
  13. peer_disconnected.connect(self._peer_disconnected)
  14. func start(url, lobby = "", mesh:=true):
  15. stop()
  16. sealed = false
  17. self.mesh = mesh
  18. self.lobby = lobby
  19. connect_to_url(url)
  20. func stop():
  21. multiplayer.multiplayer_peer = null
  22. rtc_mp.close()
  23. close()
  24. func _create_peer(id):
  25. var peer: WebRTCPeerConnection = WebRTCPeerConnection.new()
  26. peer.initialize({
  27. "iceServers": [ { "urls": ["stun:stun.l.google.com:19302"] } ]
  28. })
  29. peer.session_description_created.connect(self._offer_created.bind(id))
  30. peer.ice_candidate_created.connect(self._new_ice_candidate.bind(id))
  31. rtc_mp.add_peer(peer, id)
  32. if id < rtc_mp.get_unique_id(): # So lobby creator never creates offers.
  33. peer.create_offer()
  34. return peer
  35. func _new_ice_candidate(mid_name, index_name, sdp_name, id):
  36. send_candidate(id, mid_name, index_name, sdp_name)
  37. func _offer_created(type, data, id):
  38. if not rtc_mp.has_peer(id):
  39. return
  40. print("created", type)
  41. rtc_mp.get_peer(id).connection.set_local_description(type, data)
  42. if type == "offer": send_offer(id, data)
  43. else: send_answer(id, data)
  44. func _connected(id, use_mesh):
  45. print("Connected %d, mesh: %s" % [id, use_mesh])
  46. if use_mesh:
  47. rtc_mp.create_mesh(id)
  48. elif id == 1:
  49. rtc_mp.create_server()
  50. else:
  51. rtc_mp.create_client(id)
  52. multiplayer.multiplayer_peer = rtc_mp
  53. func _lobby_joined(lobby):
  54. self.lobby = lobby
  55. func _lobby_sealed():
  56. sealed = true
  57. func _disconnected():
  58. print("Disconnected: %d: %s" % [code, reason])
  59. if not sealed:
  60. stop() # Unexpected disconnect
  61. func _peer_connected(id):
  62. print("Peer connected %d" % id)
  63. _create_peer(id)
  64. func _peer_disconnected(id):
  65. if rtc_mp.has_peer(id): rtc_mp.remove_peer(id)
  66. func _offer_received(id, offer):
  67. print("Got offer: %d" % id)
  68. if rtc_mp.has_peer(id):
  69. rtc_mp.get_peer(id).connection.set_remote_description("offer", offer)
  70. func _answer_received(id, answer):
  71. print("Got answer: %d" % id)
  72. if rtc_mp.has_peer(id):
  73. rtc_mp.get_peer(id).connection.set_remote_description("answer", answer)
  74. func _candidate_received(id, mid, index, sdp):
  75. if rtc_mp.has_peer(id):
  76. rtc_mp.get_peer(id).connection.add_ice_candidate(mid, index, sdp)