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

reading websocket data returns random bytes #20

Closed
Prof1-web opened this issue Apr 12, 2021 · 0 comments
Closed

reading websocket data returns random bytes #20

Prof1-web opened this issue Apr 12, 2021 · 0 comments

Comments

@Prof1-web
Copy link

Prof1-web commented Apr 12, 2021

Script:

package main

import (
    "fmt"
    "io"
    "log"
    "net"

    "github.com/gobwas/ws"
)

func HandleConn(conn net.Conn) {
    for {
        header, err := ws.ReadHeader(conn)
        if err != nil {
            log.Fatal(err)
        }

        buf := make([]byte, header.Length)
        _, err = io.ReadFull(conn, buf)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(buf)
        fmt.Println(string(buf))
    }
}

func main() {
    ln, err := net.Listen("tcp", "localhost:8080")
    if err != nil {
        log.Fatal(err)
    }
    for {
        conn, err := ln.Accept()
        if err != nil {
            log.Fatal(err)
        }
        _, err = ws.Upgrade(conn)
        if err != nil {
            log.Fatal(err)
        }
        go HandleConn(conn)
    }
}

I do in browser console:

let socket = new WebSocket("ws://127.0.0.1:8080")
socket.send("Hello world")`

I see random bytes in the my terminal. Each call to socket.send("Hello world") return different bytes. But the length of the byte array is always equal to the length of the string. How can I fix this?

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

1 participant