monaco-languageclient icon indicating copy to clipboard operation
monaco-languageclient copied to clipboard

How do I properly close the socket?

Open MassMessage opened this issue 3 years ago • 3 comments

I need to reinstall the server but i'm not sure if i need to do anything but close from WebSocket? maybe i need to list to some event when the close is done then i call restart the server? that webSocket implementation seems to be part of monaco hence i'm posting here, not sure if this is the correct place...

i'm creating it like below. the expect behavior is i call Setup("my folder 1"), Setup("my folder 2") etc it close the current socket and start a new one with the new folder to pass to Install()


export function Setup(projectFolder?: string) {

    if(webSocket) {
        webSocket.close();
        webSocket = null;
    }
    
    const options:MonacoServices.Options = {};
    if(projectFolder) {
        const workspaceUrl = FilePathToURL(projectFolder);
        options.rootUri = workspaceUrl;
    }

    // install Monaco language client services
    MonacoServices.install(monaco, options);

    // create the web socket
    const url = createUrl(`ws://localhost:${wsServerPort}/foo`);
    webSocket = createWebSocket(url);

    // listen when the web socket is opened
    listen({
        webSocket,
        onConnection: connection => {
            // create and start the language client
            const languageClient = createLanguageClient(connection);
            const disposable = languageClient.start();
            connection.onClose(() => disposable.dispose());
        }
    });
}

function createLanguageClient(connection: MessageConnection): MonacoLanguageClient {
    return new MonacoLanguageClient({
        name: "Sample Language Client",
        clientOptions: {
            // use a language id as a document selector
            documentSelector: ['c'],
            // disable the default error handler
            errorHandler: {
                error: () => ErrorAction.Continue,
                closed: () => CloseAction.DoNotRestart
            }
        },
        // create a language client connection from the JSON RPC connection on demand
        connectionProvider: {
            get: (errorHandler, closeHandler) => {
                return Promise.resolve(createConnection(connection, errorHandler, closeHandler))
            }
        }
    });
}

function createUrl(path: string): string {
    return normalizeUrl(path);
}

function createWebSocket(url: string): WebSocket {
    const socketOptions = {
        maxReconnectionDelay: 10000,
        minReconnectionDelay: 1000,
        reconnectionDelayGrowFactor: 1.3,
        connectionTimeout: 10000,
        maxRetries: Infinity,
        debug: false
    };
    return new (ReconnectingWebSocket as any)(url, [], socketOptions) as WebSocket;
}

MassMessage avatar Jun 17 '22 02:06 MassMessage

What do you mean by reinstall?

There is a stop method on the LanguageClient

If you are talking about the services, MonacoServices.install returns a disposable

CGNonofr avatar Jun 17 '22 08:06 CGNonofr

What do you mean by reinstall?

i mean restart, sorry

There is a stop method on the LanguageClient

should i just use it instead of close the socket or do both?

If you are talking about the services, MonacoServices.install returns a disposable

gonna call it too. i'm talking about the language client, web socket and MonacoServices.install. In order to restart the server i should call .close() in the web socket, .stop() in the language client and .dispose() from MonacoServices.install()?

MassMessage avatar Jun 17 '22 16:06 MassMessage

.stop() is the more graceful way to shutdown the client. The client includes a close/error management that will try to reconnect if an error occurs, depending on the errorHandler in the clientOptions.

CGNonofr avatar Jun 21 '22 09:06 CGNonofr

@MassMessage I am closing the issue. If there are further questions please open a discussion: https://github.com/TypeFox/monaco-languageclient/discussions

Thank you

kaisalmen avatar Oct 19 '22 06:10 kaisalmen