Home golang Problems with reading from WebSocket Golang

Problems with reading from WebSocket Golang

Author

Date

Category

I write a server with Webskers on Golang. Pressed a simple test 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)
    _, er = 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 a browser console:

Let Socket = New WebSocket ("WS: //127.0.0.1: 8080")
Socket.send ("Hello World")

and in the terminal I have some randomy bytes. Each time Socket.send ("Hello World") is different. What is it and why do they change with every shipment? Where does he take these bytes? The number of bytes always corresponds to the number of characters in the string, i.e. He reads what it is necessary. Did as in the documentation


Answer 1, Authority 100%

data come encrypted, you need to decrypt them.

Func Handleconn (Conn net.conn) {
  for {
    // Primea Sima
    // WS.ReadHeader (Conn)
    // MSG, _, ERR: = WSUTIL.ReadClientData (Conn)
    // IF ERR! = nil {
    // Panic (ERR)
    //}
    //
    // FMT.PrintLN (MSG)
    // FMT.PrintLN (String (MSG))
    // Example a little complicated
    Header, ERR: = WS.ReadHeader (Conn)
    IF ERR! = nil {
      log.fatal (ERR)
    }
    BUF: = MAKE ([] byte, Header.Length)
    _, er = io.Readfull (Conn, BUF)
    IF ERR! = nil {
      log.fatal (ERR)
    }
    // That was added
    IF Header.masked {
      WS.cipher (BUF, Header.mask, 0)
    }
    FMT.PrintLN (BUF)
    FMT.PrintLN (String (BUF))
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions