Chat features
This commit is contained in:
@@ -1,14 +1,36 @@
|
||||
extends Control
|
||||
|
||||
onready var location = JavaScript.get_interface("location")
|
||||
|
||||
func _init():
|
||||
WebsocketController.connect("hostjoin", self, "_on_hostjoin")
|
||||
WebsocketController.connect("connected", self, "_on_connected")
|
||||
WebsocketController.connect("join_ok", self, "_on_join_ok")
|
||||
WebsocketController.connect("room_code", self, "_on_room_code")
|
||||
WebsocketController.connect("chat", self, "_on_chat")
|
||||
|
||||
func _ready():
|
||||
WebsocketController.connect_websocket()
|
||||
|
||||
func _on_hostjoin():
|
||||
var location = JavaScript.get_interface("location")
|
||||
func _on_connected():
|
||||
if location and location.hash != "":
|
||||
WebsocketController.join_game(location.hash)
|
||||
WebsocketController.join_game(location.hash.trim_prefix("#"))
|
||||
else:
|
||||
WebsocketController.host_game()
|
||||
|
||||
func _on_room_code(code):
|
||||
location.hash = code
|
||||
$CopyBtn.disabled = false
|
||||
|
||||
func _on_join_ok():
|
||||
$CopyBtn.disabled = false
|
||||
|
||||
func _on_chat(msg):
|
||||
$Chat/TextEdit.text += msg
|
||||
$Chat/TextEdit.text += "\n"
|
||||
|
||||
func _on_CopyBtn_pressed():
|
||||
OS.clipboard = location.href
|
||||
|
||||
func _on_LineEdit_text_entered(text):
|
||||
WebsocketController.chat(text)
|
||||
$Chat/LineEdit.clear()
|
||||
|
||||
29
Scripts/Message.gd
Normal file
29
Scripts/Message.gd
Normal file
@@ -0,0 +1,29 @@
|
||||
extends Object
|
||||
|
||||
class_name Message
|
||||
|
||||
var command: String
|
||||
var args: Array
|
||||
|
||||
func _init(text: String = ""):
|
||||
if text == "":
|
||||
command = ""
|
||||
args = []
|
||||
return
|
||||
var match_re = RegEx.new();
|
||||
# r"((\\,|[^,])+)"
|
||||
match_re.compile("(\\\\,|[^,])+")
|
||||
|
||||
var radix = text.find(':')
|
||||
command = text.substr(0, radix)
|
||||
var args_raw = text.substr(radix+1)
|
||||
args = []
|
||||
for match_ in match_re.search_all(args_raw):
|
||||
var arg = match_.strings[0].strip_edges().replace("\\,", ",")
|
||||
args.push_back(arg)
|
||||
|
||||
func _to_string():
|
||||
var text = "%s:" % self.command
|
||||
for arg in self.args:
|
||||
text += arg.replace(",", "\\,")
|
||||
return text
|
||||
@@ -1,38 +1,56 @@
|
||||
extends Node
|
||||
|
||||
var client = WebSocketClient.new()
|
||||
var peer
|
||||
var ws
|
||||
|
||||
signal hostjoin
|
||||
signal connected
|
||||
signal chat
|
||||
signal room_code
|
||||
signal join_ok
|
||||
|
||||
func _init():
|
||||
client.connect("connection_established", self, "_on_connect")
|
||||
client.connect("data_received", self, "_on_data")
|
||||
|
||||
func _on_connect(_proto):
|
||||
peer = client.get_peer(1)
|
||||
peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
|
||||
ws = client.get_peer(1)
|
||||
ws.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)
|
||||
emit_signal("connected")
|
||||
|
||||
func _on_data():
|
||||
var msg = peer.get_packet().get_string_from_utf8()
|
||||
var regex = RegEx.new()
|
||||
regex.compile("([\\w_]+):")
|
||||
var res = regex.search(msg)
|
||||
match res.strings[1]:
|
||||
"HOSTJOIN":
|
||||
emit_signal("hostjoin")
|
||||
var msg = Message.new(ws.get_packet().get_string_from_utf8())
|
||||
match msg.command:
|
||||
"CHAT":
|
||||
emit_signal("chat", msg.args[0])
|
||||
"ROOM_CODE":
|
||||
emit_signal("room_code", msg.args[0])
|
||||
"JOIN_OK":
|
||||
emit_signal("join_ok")
|
||||
_:
|
||||
print("Unknown command => ", msg)
|
||||
|
||||
func _process(_delta):
|
||||
client.poll()
|
||||
|
||||
func send_message(message):
|
||||
peer.put_packet(message.to_utf8())
|
||||
ws.put_packet(str(message).to_utf8())
|
||||
|
||||
func connect_websocket():
|
||||
client.connect_to_url(ProjectSettings["global/server_url"]);
|
||||
|
||||
func host_game():
|
||||
send_message("HOST:")
|
||||
var msg = Message.new()
|
||||
msg.command = "HOST"
|
||||
send_message(msg)
|
||||
|
||||
func join_game(hash_):
|
||||
send_message("JOIN: %s" % hash_)
|
||||
var msg = Message.new()
|
||||
msg.command = "JOIN"
|
||||
msg.args.push_back(hash_)
|
||||
send_message(msg)
|
||||
|
||||
func chat(text):
|
||||
var msg = Message.new()
|
||||
msg.command = "CHAT"
|
||||
msg.args.push_back(text)
|
||||
send_message(msg)
|
||||
|
||||
Reference in New Issue
Block a user