Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Using websocket error :The script will never generate a response. #407

Open
1 task done
luo12yan opened this issue Nov 2, 2023 · 1 comment
Open
1 task done

Comments

@luo12yan
Copy link

luo12yan commented Nov 2, 2023

Is there an existing issue for this?

  • I have searched the existing issues

What version of workers-rs are you using?

0.0.18

What version of wrangler are you using?

3.5.1

Describe the bug

I am testing websocket with the following code

use futures_util::StreamExt;
use worker::*;

async fn index<D>(_: Request, _: RouteContext<D>) -> Result<Response> {
    Response::from_html(INDEX_HTML)
}

async fn websocket<D>(_req: Request, _: RouteContext<D>) -> Result<Response> {
    let pair = WebSocketPair::new()?;
    let server = pair.server;
    server.accept()?;

    wasm_bindgen_futures::spawn_local(async move {
        let mut event_stream = server.events().expect("could not open stream");

        while let Some(data)=event_stream.next().await {
            let event=data.unwrap();
            let _ = match event {
                WebsocketEvent::Message(msg) => server.send(&msg.text().unwrap()),
                WebsocketEvent::Close(close) => {
                    server.close(Some(close.code()), Some(close.reason()))
                },
            };
        }
    });

    Response::from_websocket(pair.client)
}

#[event(fetch)]
async fn main(req: Request, env: Env, _ctx: Context) -> Result<Response> {
    let router = Router::new();

    router
        .on_async("/", index)
        .on_async("/chat", websocket)
        .run(req, env)
        .await
}

static INDEX_HTML: &str = r#"<!DOCTYPE html>
<html>
    <head>
        <title>WS Chat</title>
    </head>
    <body>
        <h1>WS Chat</h1>
        <div id="chat">
            <p><em>Connecting...</em></p>
        </div>
        <input type="text" id="text" />
        <button type="button" id="submit">Submit</button>
        <script>
            const chat = document.getElementById('chat');
            const msg = document.getElementById('msg');
            const submit = document.getElementById('submit');
            const ws = new WebSocket(`ws://${location.host}/chat`);

            ws.onopen = function() {
                chat.innerHTML = '<p><em>Connected!</em></p>';
            };

            ws.onmessage = function(msg) {
                showMessage(msg.data);
            };

            ws.onclose = function() {
                chat.getElementsByTagName('em')[0].innerText = 'Disconnected!';
            };

            submit.onclick = function() {
                const msg = text.value;
                ws.send(msg);
                text.value = '';

                showMessage('<You>: ' + msg);
            };
            function showMessage(data) {
                const line = document.createElement('p');
                line.innerText = data;
                chat.appendChild(line);
            }
        </script>
    </body>
</html>
"#;

Steps To Reproduce

1、npm run dev

2、open a browser and access

3、send message

4、close browser

5、error:

workerd/server/server.c++:2528: error: Uncaught exception: kj/compat/http.c++:3910: disconnected: worker_do_not_log; Request failed due to internal error
stack: 0 0 7ff79f51dc1a 0 0 0 7ff79f51dbcd 0 0 0

@kflansburg
Copy link
Member

Can you update wrangler (npm update -g wrangler) and also confirm you are using worker 0.0.18. I was not able to reproduce this and something like this was fixed in 0.0.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants