Skip to content

Commit

Permalink
use native websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
ender-null committed May 22, 2024
1 parent 2bb9e59 commit f11653c
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/plugins/spica.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { WebSocket } from 'ws';
import { Bot } from '../bot';
import { PluginBase } from '../plugin';
import { Message } from '../types';
Expand Down Expand Up @@ -43,7 +42,7 @@ export class SpicaPlugin extends PluginBase {
const ws: WebSocket = new WebSocket('wss://spica.end.works/wired');
logger.info('after websocket');

ws.on('ready', () => {
ws.onopen = () => {
logger.info('spica ready');
if (isCommand(this, 1, msg.content)) {
ws.send('status');
Expand All @@ -52,19 +51,20 @@ export class SpicaPlugin extends PluginBase {
} else if (isCommand(this, 3, msg.content)) {
ws.send(input);
}
});
};

ws.on('error', async (error: ErrorEvent) => {
ws.onerror = (error: ErrorEvent) => {
logger.info('spica error');
logger.error(error);
});
};

ws.on('close', (code) => {
ws.onclose = (code) => {
logger.info(`spica close ${code}`);
});
};

ws.on('message', (data: string) => {
ws.onmessage = (ev: MessageEvent) => {
logger.info('spica message');
const data = ev.data as string;
let text = this.bot.errors.noResults;
if (isCommand(this, 1, msg.content)) {
const result = JSON.parse(data.slice(7));
Expand All @@ -85,6 +85,6 @@ export class SpicaPlugin extends PluginBase {
}
this.bot.replyMessage(msg, text);
ws.close();
});
};
}
}

0 comments on commit f11653c

Please sign in to comment.