Start Langium as standalone server #471
-
Hi!
But it doesn't seem to work. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hi @theogiraudet, That's weird, I just tried to use |
Beta Was this translation helpful? Give feedback.
-
You can connect Langium directly to a WebSocket with a custom message reader / writer. The following snippet is based on the class WebSocketMessageWriter extends AbstractMessageWriter {
private readonly ws: WebSocket;
constructor(ws: WebSocket) {
super();
this.ws = ws;
}
write(msg: Message): Promise<void> {
return new Promise((resolve, reject) => {
const content = JSON.stringify(msg);
this.ws.send(content, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
end(): void {}
}
class WebSocketMessageReader extends AbstractMessageReader {
private readonly ws: WebSocket;
constructor(ws: WebSocket) {
super();
this.ws = ws;
}
listen(callback: DataCallback): Disposable {
const messageListener = (rawMessage: Message) => {
const message = JSON.parse(rawMessage.toString()) as Message;
callback(message);
};
const closeListener = () => this.fireClose()
this.ws.on('message', messageListener);
this.ws.on('close', closeListener);
return {
dispose: () => {
this.ws.removeListener('message', messageListener);
this.ws.removeListener('close', closeListener);
}
};
}
}
const wsServer = new WebSocket.Server(/* options */);
wsServer.on('connection', (ws, request) => {
const reader = new WebSocketMessageReader(ws);
const writer = new WebSocketMessageWriter(ws);
const connection = createConnection(reader, writer);
const services = createMyLanguageServices({ connection });
startLanguageServer(services.shared);
}); |
Beta Was this translation helpful? Give feedback.
You can connect Langium directly to a WebSocket with a custom message reader / writer. The following snippet is based on the
ws
package: