-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainScript.cs
More file actions
101 lines (86 loc) · 2.75 KB
/
mainScript.cs
File metadata and controls
101 lines (86 loc) · 2.75 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Godot;
using System;
using System.Text;
using System.Threading;
public class mainScript : Node2D
{
private chat ch = new chat();
private Godot.TextEdit wsURI = new Godot.TextEdit();
private Button btn = new Button();
private static WebSocketThread websocket;
private static System.Threading.Thread wsThreadControl;
private Godot.Error wsThreadError;
string checkInstanceOf;
public override void _Ready()
{
ch = (chat)GetNode("MarginContainer/VBoxContainer/chat");
btn = (Button)GetNode("MarginContainer/VBoxContainer/HBoxContainer/Button");
wsURI = (Godot.TextEdit)GetNode("MarginContainer/VBoxContainer/HBoxContainer/TextEdit");
ch.Println(wsURI.GetType().ToString());
ch.Println("Ready");
checkInstanceOf = "Blahblah";
// Instantiate websocket with a reference to calling class
websocket = NewWebSocketThread();
// this must come after
wsThreadControl = NewWsThreadControl();
updateButton();
}
private WebSocketThread NewWebSocketThread() {
WebSocketThread ws;
ws = new WebSocketThread(GetConnectionURI());
// wire connections
ws.Connect("SignalSendMessage", this, nameof(Println));
ws.Connect("SignalReturnStringPacket", this, nameof(TextPkt));
// new thread control
return ws;
}
private System.Threading.Thread NewWsThreadControl() {
return new System.Threading.Thread(new ThreadStart(websocket.Init));
}
private void startWs() {
if (wsThreadControl.IsAlive) {
// stop our thread
websocket.StopThread();
// join the thread
wsThreadControl.Join();
}
// new websocket object (reset)
websocket = NewWebSocketThread();
// new thread control
wsThreadControl = NewWsThreadControl();
// start the method
wsThreadControl.Start();
}
public string GetConnectionURI() {
return wsURI.Text;
}
private void updateButton() {
if (websocket.IsPolling) {
btn.Text = "Disconnect";
} else {
btn.Text = "Connect";
}
}
public void Println(string msg) {
ch.Println(msg);
}
public void TextPkt(string msg) {
ch.Println("Text Packet: " + msg);
}
private void _on_Button_button_up()
{
if (!websocket.IsPolling) {
ch.ClearDialog();
startWs();
} else {
websocket.StopThread();
// block until we return
wsThreadControl.Join();
wsThreadControl = NewWsThreadControl();
}
updateButton();
}
public override void _Process(float delta) {
updateButton();
}
}