Skip to content

Commit 200de21

Browse files
committed
feat: Ephys Link Proxy support (#698)
* Correctly register with proxy, need to complete connection * Process getting pinpoint ID before invoking connection * Create UI for handling proxy connection * Connect to proxy UI and function * Attached UI to settings * Use correct proxy URL, fix connect button appearance, pass pinpoint ID * Use model from aquarium
1 parent 52b6276 commit 200de21

File tree

8 files changed

+1698
-600
lines changed

8 files changed

+1698
-600
lines changed

Assets/Prefabs/UI/SettingsMenu/Menus/EphysLinkMenu.prefab

Lines changed: 1491 additions & 347 deletions
Large diffs are not rendered by default.

Assets/Scenes/TrajectoryPlanner.unity

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,17 +1188,6 @@ MonoBehaviour:
11881188
m_Script: {fileID: 11500000, guid: 1f0d7476d212c8842bb7706a6d996932, type: 3}
11891189
m_Name:
11901190
m_EditorClassIdentifier:
1191-
--- !u!114 &922715042 stripped
1192-
MonoBehaviour:
1193-
m_CorrespondingSourceObject: {fileID: 7610818863979926319, guid: 734f6254569c01842b9d2e2ff1b1d7ae, type: 3}
1194-
m_PrefabInstance: {fileID: 1302158968208938923}
1195-
m_PrefabAsset: {fileID: 0}
1196-
m_GameObject: {fileID: 0}
1197-
m_Enabled: 1
1198-
m_EditorHideFlags: 0
1199-
m_Script: {fileID: 11500000, guid: d9e6156b343e276468f718e49124fa4f, type: 3}
1200-
m_Name:
1201-
m_EditorClassIdentifier:
12021191
--- !u!1 &952734154 stripped
12031192
GameObject:
12041193
m_CorrespondingSourceObject: {fileID: 206732207517579495, guid: 94cdeca105038d74ea47b57e6b99eb4e, type: 3}
@@ -1582,6 +1571,17 @@ RectTransform:
15821571
m_CorrespondingSourceObject: {fileID: 2395954049702337235, guid: 94cdeca105038d74ea47b57e6b99eb4e, type: 3}
15831572
m_PrefabInstance: {fileID: 341190943874437988}
15841573
m_PrefabAsset: {fileID: 0}
1574+
--- !u!114 &1299700020 stripped
1575+
MonoBehaviour:
1576+
m_CorrespondingSourceObject: {fileID: 995888031812256567, guid: 94cdeca105038d74ea47b57e6b99eb4e, type: 3}
1577+
m_PrefabInstance: {fileID: 341190943874437988}
1578+
m_PrefabAsset: {fileID: 0}
1579+
m_GameObject: {fileID: 0}
1580+
m_Enabled: 1
1581+
m_EditorHideFlags: 0
1582+
m_Script: {fileID: 11500000, guid: 2da0c512f12947e489f739169773d7ca, type: 3}
1583+
m_Name:
1584+
m_EditorClassIdentifier:
15851585
--- !u!114 &1301424888 stripped
15861586
MonoBehaviour:
15871587
m_CorrespondingSourceObject: {fileID: 897619903058418205, guid: 94cdeca105038d74ea47b57e6b99eb4e, type: 3}
@@ -4172,7 +4172,7 @@ PrefabInstance:
41724172
- target: {fileID: 341190945559892368, guid: 94cdeca105038d74ea47b57e6b99eb4e, type: 3}
41734173
propertyPath: _skullList.Array.data[1]
41744174
value:
4175-
objectReference: {fileID: 922715042}
4175+
objectReference: {fileID: 0}
41764176
- target: {fileID: 341190945559892372, guid: 94cdeca105038d74ea47b57e6b99eb4e, type: 3}
41774177
propertyPath: m_AnchorMax.y
41784178
value: 1
@@ -9256,6 +9256,10 @@ PrefabInstance:
92569256
propertyPath: _ghostInactiveProbesToggle
92579257
value:
92589258
objectReference: {fileID: 1719348668}
9259+
- target: {fileID: 8016510558008620337, guid: 482bfae3ba88b424e96d7fcd669f7b73, type: 3}
9260+
propertyPath: _ephysLinkProxyAddressInput
9261+
value:
9262+
objectReference: {fileID: 1299700020}
92599263
- target: {fileID: 8016510558008620337, guid: 482bfae3ba88b424e96d7fcd669f7b73, type: 3}
92609264
propertyPath: sensapexLinkServerPortInput
92619265
value:
@@ -9406,7 +9410,7 @@ PrefabInstance:
94069410
objectReference: {fileID: 0}
94079411
- target: {fileID: 8016510558008620337, guid: 482bfae3ba88b424e96d7fcd669f7b73, type: 3}
94089412
propertyPath: EphysLinkServerPortChangedEvent.m_PersistentCalls.m_Calls.Array.size
9409-
value: 1
9413+
value: 0
94109414
objectReference: {fileID: 0}
94119415
- target: {fileID: 8016510558008620337, guid: 482bfae3ba88b424e96d7fcd669f7b73, type: 3}
94129416
propertyPath: GhostInactiveProbesChangedEvent.m_PersistentCalls.m_Calls.Array.size

Assets/Scripts/EphysLink/CommunicationManager.cs

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ private void Awake()
5555
#endregion
5656

5757
#region Connection Handler
58-
5958
public void ServerSettingsLoaded()
6059
{
6160
// Automatically connect if the server credentials are possible
@@ -111,13 +110,14 @@ public void ConnectToServer(
111110
"connect",
112111
() =>
113112
{
114-
Debug.Log($"Connected to WebSocket server at {ip}:{port}");
113+
Debug.Log($"Connected to Ephys Link server at {ip}:{port}");
115114
IsConnected = true;
116115

117116
// Save settings
118117
Settings.EphysLinkServerIp = ip;
119118
Settings.EphysLinkServerPort = port;
120119

120+
// Invoke connected callback
121121
onConnected?.Invoke();
122122
}
123123
);
@@ -144,7 +144,7 @@ public void ConnectToServer(
144144
() =>
145145
{
146146
var connectionTimeoutMessage =
147-
"Connection to server at {ip}:{port} timed out";
147+
$"Connection to server at {ip}:{port} timed out";
148148
Debug.LogWarning(connectionTimeoutMessage);
149149
IsConnected = false;
150150
_connectionManager.Close();
@@ -168,6 +168,99 @@ public void ConnectToServer(
168168
}
169169
}
170170

171+
public void ConnectToProxy(
172+
string proxyAddress,
173+
string pinpointID,
174+
Action onConnected = null,
175+
Action<string> onError = null
176+
)
177+
{
178+
// Disconnect the old connection if needed
179+
if (_connectionManager != null && _connectionManager.Socket.IsOpen)
180+
_connectionManager.Close();
181+
182+
// Create new connection
183+
var options = new SocketOptions { Timeout = new TimeSpan(0, 0, 2) };
184+
185+
// Try to open a connection
186+
try
187+
{
188+
// Create a new socket
189+
_connectionManager = new SocketManager(
190+
new Uri($"http://{proxyAddress}:3000"),
191+
options
192+
);
193+
_socket = _connectionManager.Socket;
194+
195+
// On successful connection
196+
_socket.Once(
197+
"connect",
198+
() =>
199+
{
200+
Debug.Log($"Connected to proxy server at {proxyAddress}:3000");
201+
IsConnected = true;
202+
203+
// Save settings and clear Server IP (don't allow auto reconnect)
204+
Settings.EphysLinkServerIp = "";
205+
Settings.EphysLinkProxyAddress = proxyAddress;
206+
}
207+
);
208+
209+
_socket.Once(
210+
"get_pinpoint_id",
211+
() =>
212+
{
213+
_socket.EmitAck(ToJson(new PinpointIdResponse(pinpointID, true)));
214+
onConnected?.Invoke();
215+
}
216+
);
217+
218+
// On error
219+
_socket.Once(
220+
"error",
221+
() =>
222+
{
223+
var connectionErrorMessage =
224+
$"Error connecting to proxy at {proxyAddress}:3000. Check proxy for details.";
225+
Debug.LogWarning(connectionErrorMessage);
226+
IsConnected = false;
227+
_connectionManager.Close();
228+
_connectionManager = null;
229+
_socket = null;
230+
onError?.Invoke(connectionErrorMessage);
231+
}
232+
);
233+
234+
// On timeout
235+
_socket.Once(
236+
"connect_timeout",
237+
() =>
238+
{
239+
var connectionTimeoutMessage =
240+
$"Connection to proxy at {proxyAddress}:3000 timed out";
241+
Debug.LogWarning(connectionTimeoutMessage);
242+
IsConnected = false;
243+
_connectionManager.Close();
244+
_connectionManager = null;
245+
_socket = null;
246+
onError?.Invoke(connectionTimeoutMessage);
247+
}
248+
);
249+
}
250+
catch (Exception e)
251+
{
252+
// On socket generation error
253+
var connectionErrorMessage =
254+
$"Error connecting to proxy at {proxyAddress}:3000. Check proxy for details.";
255+
Debug.LogWarning(connectionErrorMessage);
256+
Debug.LogWarning("Exception: " + e);
257+
IsConnected = false;
258+
_connectionManager = null;
259+
_socket = null;
260+
onError?.Invoke(connectionErrorMessage);
261+
}
262+
}
263+
171264
/// <summary>
172265
/// Disconnect client from WebSocket server.
173266
/// </summary>
@@ -605,7 +698,7 @@ private static T ParseJson<T>(string json)
605698
return JsonUtility.FromJson<T>(json);
606699
}
607700

608-
private string ToJson<T>(T data)
701+
private static string ToJson<T>(T data)
609702
{
610703
return JsonUtility.ToJson(data);
611704
}

0 commit comments

Comments
 (0)