mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-11-17 22:29:27 +00:00
Serial Plotter implementation (#597)
* spawn new window where to instantiate serial plotter app * initialize serial monito web app * connect serial plotter app with websocket * use npm serial-plotter package * refactor monitor connection and fix some connection issues * fix clearConsole + refactor monitor connection * add serial unit tests * refactoring and cleaning code
This commit is contained in:
committed by
GitHub
parent
9863dc2f90
commit
20f7712129
@@ -0,0 +1,46 @@
|
||||
import { Emitter } from '@theia/core';
|
||||
import { injectable } from 'inversify';
|
||||
import * as WebSocket from 'ws';
|
||||
import { WebSocketService } from './web-socket-service';
|
||||
|
||||
@injectable()
|
||||
export default class WebSocketServiceImpl implements WebSocketService {
|
||||
protected wsClients: WebSocket[];
|
||||
protected server: WebSocket.Server;
|
||||
|
||||
protected readonly onMessage = new Emitter<string>();
|
||||
public readonly onMessageReceived = this.onMessage.event;
|
||||
|
||||
constructor() {
|
||||
this.wsClients = [];
|
||||
this.server = new WebSocket.Server({ port: 0 });
|
||||
|
||||
const addClient = this.addClient.bind(this);
|
||||
this.server.on('connection', addClient);
|
||||
}
|
||||
|
||||
private addClient(ws: WebSocket): void {
|
||||
this.wsClients.push(ws);
|
||||
ws.onclose = () => {
|
||||
this.wsClients.splice(this.wsClients.indexOf(ws), 1);
|
||||
};
|
||||
|
||||
ws.onmessage = (res) => {
|
||||
this.onMessage.fire(res.data.toString());
|
||||
};
|
||||
}
|
||||
|
||||
getAddress(): WebSocket.AddressInfo {
|
||||
return this.server.address() as WebSocket.AddressInfo;
|
||||
}
|
||||
|
||||
sendMessage(message: string): void {
|
||||
this.wsClients.forEach((w) => {
|
||||
try {
|
||||
w.send(message);
|
||||
} catch {
|
||||
w.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Event } from '@theia/core/lib/common/event';
|
||||
import * as WebSocket from 'ws';
|
||||
|
||||
export const WebSocketService = Symbol('WebSocketService');
|
||||
export interface WebSocketService {
|
||||
getAddress(): WebSocket.AddressInfo;
|
||||
sendMessage(message: string): void;
|
||||
onMessageReceived: Event<string>;
|
||||
}
|
||||
Reference in New Issue
Block a user