BREAKING CHANGE(socketconnection): Stricter typings, smartserve hooks, connection fixes, and tag API change
This commit is contained in:
@@ -26,7 +26,7 @@ export interface ISocketConnectionConstructorOptions {
|
||||
authenticated: boolean;
|
||||
side: TSocketConnectionSide;
|
||||
smartsocketHost: Smartsocket | SmartsocketClient;
|
||||
socket: WebSocket | pluginsTyped.ws.WebSocket;
|
||||
socket: pluginsTyped.TWebSocket | pluginsTyped.IWebSocketLike;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,7 +47,7 @@ export class SocketConnection {
|
||||
public side: TSocketConnectionSide;
|
||||
public authenticated: boolean = false;
|
||||
public smartsocketRef: Smartsocket | SmartsocketClient;
|
||||
public socket: WebSocket | pluginsTyped.ws.WebSocket;
|
||||
public socket: pluginsTyped.TWebSocket | pluginsTyped.IWebSocketLike;
|
||||
|
||||
public eventSubject = new plugins.smartrx.rxjs.Subject<interfaces.TConnectionStatus>();
|
||||
public eventStatus: interfaces.TConnectionStatus = 'new';
|
||||
@@ -82,13 +82,13 @@ export class SocketConnection {
|
||||
public handleMessage(messageData: interfaces.ISocketMessage): void {
|
||||
switch (messageData.type) {
|
||||
case 'function':
|
||||
this.handleFunctionCall(messageData);
|
||||
this.handleFunctionCall(messageData as interfaces.ISocketMessage<interfaces.IFunctionCallPayload>);
|
||||
break;
|
||||
case 'functionResponse':
|
||||
this.handleFunctionResponse(messageData);
|
||||
this.handleFunctionResponse(messageData as interfaces.ISocketMessage<interfaces.IFunctionCallPayload>);
|
||||
break;
|
||||
case 'tagUpdate':
|
||||
this.handleTagUpdate(messageData);
|
||||
this.handleTagUpdate(messageData as interfaces.ISocketMessage<interfaces.ITagUpdatePayload>);
|
||||
break;
|
||||
default:
|
||||
// Authentication messages are handled by the server/client classes
|
||||
@@ -96,7 +96,7 @@ export class SocketConnection {
|
||||
}
|
||||
}
|
||||
|
||||
private handleFunctionCall(messageData: interfaces.ISocketMessage): void {
|
||||
private handleFunctionCall(messageData: interfaces.ISocketMessage<interfaces.IFunctionCallPayload>): void {
|
||||
const requestData: ISocketRequestDataObject<any> = {
|
||||
funcCallData: {
|
||||
funcName: messageData.payload.funcName,
|
||||
@@ -123,7 +123,7 @@ export class SocketConnection {
|
||||
}
|
||||
}
|
||||
|
||||
private handleFunctionResponse(messageData: interfaces.ISocketMessage): void {
|
||||
private handleFunctionResponse(messageData: interfaces.ISocketMessage<interfaces.IFunctionCallPayload>): void {
|
||||
const responseData: ISocketRequestDataObject<any> = {
|
||||
funcCallData: {
|
||||
funcName: messageData.payload.funcName,
|
||||
@@ -141,7 +141,7 @@ export class SocketConnection {
|
||||
}
|
||||
}
|
||||
|
||||
private handleTagUpdate(messageData: interfaces.ISocketMessage): void {
|
||||
private handleTagUpdate(messageData: interfaces.ISocketMessage<interfaces.ITagUpdatePayload>): void {
|
||||
const tagStoreArg = messageData.payload.tags as interfaces.TTagStore;
|
||||
if (!plugins.smartjson.deepEqualObjects(this.tagStore, tagStoreArg)) {
|
||||
this.tagStore = tagStoreArg;
|
||||
@@ -181,17 +181,16 @@ export class SocketConnection {
|
||||
}
|
||||
|
||||
/**
|
||||
* gets a tag by id
|
||||
* @param tagIdArg
|
||||
* Gets a tag by id
|
||||
*/
|
||||
public async getTagById(tagIdArg: interfaces.ITag['id']) {
|
||||
public getTagById(tagIdArg: interfaces.ITag['id']): interfaces.ITag | undefined {
|
||||
return this.tagStore[tagIdArg];
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a tag from a connection
|
||||
* Removes a tag from a connection
|
||||
*/
|
||||
public async removeTagById(tagIdArg: interfaces.ITag['id']) {
|
||||
public removeTagById(tagIdArg: interfaces.ITag['id']): void {
|
||||
delete this.tagStore[tagIdArg];
|
||||
this.tagStoreObservable.next(this.tagStore);
|
||||
this.sendMessage({
|
||||
@@ -209,7 +208,7 @@ export class SocketConnection {
|
||||
const done = plugins.smartpromise.defer<SocketConnection>();
|
||||
|
||||
// Set up message handler for authentication
|
||||
const messageHandler = (event: MessageEvent | { data: string }) => {
|
||||
const messageHandler = (event: pluginsTyped.TMessageEvent) => {
|
||||
try {
|
||||
const data = typeof event.data === 'string' ? event.data : event.data.toString();
|
||||
const message: interfaces.ISocketMessage = JSON.parse(data);
|
||||
@@ -241,11 +240,11 @@ export class SocketConnection {
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
// Not a valid message, ignore
|
||||
logger.log('warn', `Failed to parse auth message: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
};
|
||||
|
||||
this.socket.addEventListener('message', messageHandler as any);
|
||||
(this.socket as pluginsTyped.IWebSocketLike).addEventListener('message', messageHandler);
|
||||
|
||||
// Request authentication
|
||||
const requestAuthPayload: interfaces.TAuthRequestMessage = {
|
||||
@@ -268,17 +267,17 @@ export class SocketConnection {
|
||||
const done = plugins.smartpromise.defer();
|
||||
if (this.authenticated) {
|
||||
// Set up message handler for all messages
|
||||
const messageHandler = (event: MessageEvent | { data: string }) => {
|
||||
const messageHandler = (event: pluginsTyped.TMessageEvent) => {
|
||||
try {
|
||||
const data = typeof event.data === 'string' ? event.data : event.data.toString();
|
||||
const message: interfaces.ISocketMessage = JSON.parse(data);
|
||||
this.handleMessage(message);
|
||||
} catch (err) {
|
||||
// Not a valid JSON message, ignore
|
||||
logger.log('warn', `Failed to parse socket message: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
};
|
||||
|
||||
this.socket.addEventListener('message', messageHandler as any);
|
||||
(this.socket as pluginsTyped.IWebSocketLike).addEventListener('message', messageHandler);
|
||||
|
||||
logger.log(
|
||||
'info',
|
||||
|
||||
Reference in New Issue
Block a user