Skip to content

Commit

Permalink
feat(reconnecting): if the connection to the language server is close…
Browse files Browse the repository at this point in the history
…d the connection is restarted
  • Loading branch information
st-vi committed Nov 22, 2023
1 parent 4d3d63b commit f91a80b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions WebSocketClient/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<body>
<div class="header">
<h1>UVL Playground</h1>
<h2 id="connection" style="color: red;"></h2>
</div>
<div class="flex-container">
<div class="container">
Expand Down
24 changes: 17 additions & 7 deletions WebSocketClient/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const languageId = 'uvls';
let languageClient: MonacoLanguageClient;
let fileID;
let model;
const connectionText = document.getElementById("connection");

const createUrl = (hostname: string, port: number, path: string, searchParams: Record<string, any> = {}, secure: boolean): string => {
const protocol = secure ? 'wss' : 'ws';
Expand All @@ -49,7 +50,18 @@ const createUrl = (hostname: string, port: number, path: string, searchParams: R

const createWebSocket = (url: string): WebSocket => {
const webSocket = new WebSocket(url);
webSocket.onerror = (x) => {
if(connectionText){
connectionText.textContent = "Could not connect to language server. Reconnecting ...";
}
setTimeout(() => {
createWebSocket(url);
}, 1000);
};
webSocket.onopen = async () => {
if(connectionText){
connectionText.textContent = "";
}
const socket = toSocket(webSocket);
const reader = new WebSocketMessageReader(socket);
const writer = new WebSocketMessageWriter(socket);
Expand All @@ -58,7 +70,10 @@ const createWebSocket = (url: string): WebSocket => {
writer
});
await languageClient.start();
reader.onClose(() => languageClient.stop());
reader.onClose(() => {
languageClient.stop();
createWebSocket(url);
});
};
return webSocket;
};
Expand Down Expand Up @@ -87,7 +102,6 @@ const createLanguageClient = (transports: MessageTransports): MonacoLanguageClie
messageStrategy: {
handleMessage(message: Message, next: (message: Message) => void) {
if(Message.isRequest(message)){
console.log("Sending Request");
const m: any = message;
if(m.method === 'workspace/executeCommand' && m.params.command === 'uvls.open_web'){
const configUri: string = m.params.arguments[0].uri;
Expand All @@ -96,17 +110,14 @@ const createLanguageClient = (transports: MessageTransports): MonacoLanguageClie
if (window.location.protocol === "https:") {
protocoll = 'https';
}
console.log(window.location.protocol);
const newUrl: string = `${protocoll}://${config.languageServerHostName}:${url.port}${url.pathname}`;
console.log(newUrl);
const iframeContainer: any = document.getElementById('iframeContainer');
const myIframe: any = document.getElementById('myIframe');
iframeContainer.style.display = 'block';
myIframe.src = newUrl;
}
}
if(Message.isResponse(message)){
console.log("Receiving Response");
let result = message.result;
if(typeof result === "string" || result instanceof String){
if(result.startsWith("digraph")){
Expand All @@ -115,10 +126,8 @@ const createLanguageClient = (transports: MessageTransports): MonacoLanguageClie
}
}
if(Message.isNotification(message)){
console.log("Notification Message");
}

console.log(message);
next(message);
}
}
Expand Down Expand Up @@ -199,6 +208,7 @@ export const startPythonClient = async () => {
}, location.protocol === 'https:'));



// use the file create before
const modelRef = await createModelReference(monaco.Uri.file(`/workspace/${fileID}.uvl`));
model = modelRef.object;
Expand Down

0 comments on commit f91a80b

Please sign in to comment.