Skip to content

Commit 8160b73

Browse files
author
Tim Etchells
committed
Handle socket namespacing
For compatibility with Microclimate 19.3
1 parent edbe744 commit 8160b73

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

dev/src/command/NewConnectionCmd.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,15 @@ export async function onSuccessfulConnection(mcUri: vscode.Uri, host: string, mc
183183
const rawWorkspace: string = mcEnvData.workspace_location;
184184
const rawPlatform: string = mcEnvData.os_platform;
185185

186+
// user_string and socket_namespace are the same on ICP, except latter starts with /
187+
// on local, user_string is null, and socket_namespace is "/default".
188+
// const rawUser: string = mcEnvData.user_string || "";
189+
const rawSocketNS: string = mcEnvData.socket_namespace || "";
190+
186191
Log.d("rawVersion from Microclimate is", rawVersion);
187192
Log.d("rawWorkspace from Microclimate is", rawWorkspace);
188193
Log.d("rawPlatform from Microclimate is", rawPlatform);
194+
Log.d("rawSocketNS from Microclimate is", rawSocketNS);
189195
if (rawVersion == null || rawWorkspace == null) {
190196
Log.e("Microclimate environment did not provide either version or workspace. Data provided is:", mcEnvData);
191197
throw new Error(Translator.t(STRING_NS, "versionNotProvided", { requiredVersion: MCEnvironment.REQUIRED_VERSION_STR }));
@@ -202,8 +208,11 @@ export async function onSuccessfulConnection(mcUri: vscode.Uri, host: string, mc
202208

203209
const versionNum = MCEnvironment.getVersionNumber(mcEnvData);
204210

211+
// normalize namespace so it doesn't start with '/'
212+
const socketNS = rawSocketNS.startsWith("/") ? rawSocketNS.substring(1, rawSocketNS.length) : rawSocketNS;
213+
205214
try {
206-
return await ConnectionManager.instance.addConnection(mcUri, host, versionNum, workspace);
215+
return await ConnectionManager.instance.addConnection(mcUri, host, versionNum, socketNS, workspace);
207216
}
208217
catch (err) {
209218
Log.i("New connection rejected by ConnectionManager ", err.message || err);

dev/src/microclimate/connection/Connection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ export default class Connection implements ITreeItemAdaptable, vscode.QuickPickI
5454
public readonly mcUri: vscode.Uri,
5555
public readonly host: string,
5656
public readonly version: number,
57+
public readonly socketNS: string,
5758
workspacePath_: string
5859
) {
5960
this.projectsApiUri = Endpoints.getEndpoint(this, Endpoints.PROJECTS);
60-
this.socket = new MCSocket(mcUri.toString(), this);
61+
this.socket = new MCSocket(this, socketNS);
6162
this.logManager = new MCLogManager(this);
6263
this.workspacePath = vscode.Uri.file(workspacePath_);
6364
this.versionStr = MCEnvironment.getVersionAsString(version);

dev/src/microclimate/connection/ConnectionManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class ConnectionManager {
4747
return this._connections;
4848
}
4949

50-
public async addConnection(uri: vscode.Uri, host: string, mcVersion: number, workspace: string): Promise<Connection> {
50+
public async addConnection(uri: vscode.Uri, host: string, mcVersion: number, socketNS: string, workspace: string): Promise<Connection> {
5151
if (this.connectionExists(uri)) {
5252
const alreadyExists = Translator.t(StringNamespaces.DEFAULT, "connectionAlreadyExists", { uri });
5353
// Log.i(alreadyExists);
@@ -56,7 +56,7 @@ export default class ConnectionManager {
5656

5757
// all validation that this connection is good must be done by this point
5858

59-
const newConnection: Connection = new Connection(uri, host, mcVersion, workspace);
59+
const newConnection: Connection = new Connection(uri, host, mcVersion, socketNS, workspace);
6060
Log.i("New Connection @ " + uri);
6161
this._connections.push(newConnection);
6262
ConnectionManager.saveConnections();

dev/src/microclimate/connection/MCSocket.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,33 @@ import SocketEvents from "./SocketEvents";
2424
*/
2525
export default class MCSocket {
2626

27+
private readonly uri: string;
2728
private readonly socket: SocketIOClient.Socket;
2829

30+
/**
31+
* Create a SocketIO connection to the server at the given URI.
32+
* Can throw an error.
33+
*
34+
* @param namespace - Socket namespace. Must not start with a slash. Can be the empty string.
35+
*/
2936
constructor(
30-
public readonly uri: string,
31-
private readonly connection: Connection
37+
private readonly connection: Connection,
38+
namespace: string,
3239
) {
33-
Log.i("Creating MCSocket for URI", uri);
34-
this.socket = io(uri);
40+
this.uri = connection.mcUri.toString();
41+
if (namespace) {
42+
if (!this.uri.endsWith("/")) {
43+
this.uri += "/";
44+
}
45+
this.uri += namespace;
46+
}
47+
Log.i("Creating MCSocket for URI", this.uri);
48+
49+
const options: SocketIOClient.ConnectOpts = {
50+
// rejectUnauthorized:
51+
};
52+
53+
this.socket = io(this.uri, options);
3554

3655
this.socket.connect();
3756

0 commit comments

Comments
 (0)