Go websockets 数据 gopherjs
Go websockets data gopherjs
我目前正在尝试使用 websockets 进行通信,我的代码如下(我使用的是大猩猩)
buff := bytes.NewBuffer([]byte{})
binary.Write(buff, binary.LittleEndian, uint64(1))
binary.Write(buff, binary.LittleEndian, len(message))
binary.Write(buff, binary.LittleEndian, message)
client.Write <- buff.Bytes()
c.Write 通道在 for select 循环中
case msg := <-client.Write:
buffer := &bytes.Buffer{}
err := binary.Write(buffer, binary.LittleEndian, msg)
if err != nil {
return
}
err = client.Ws.WriteMessage(websocket.BinaryMessage, buffer.Bytes())
if err != nil {
return
}
buffer.Reset()
而客户端只是一个结构体
type Client struct {
Ws *websocket.Conn
Read chan []byte
Write chan []byte
Account *models.Account
}
消息发送成功,我是这样看的
b, err := conn.Read(buff)
if err != nil {
utils.Log("Error while reading from socket " + err.Error())
return
}
buffer := bytes.NewBuffer(buff[:b])
t, err := binary.ReadUvarint(buffer)
utils.Log(t)
if err != nil {
utils.Log("Error while reading from socket " + err.Error())
return
}
switch t {
case 1:
roomsLen, err := binary.ReadUvarint(buffer)
if err != nil {
utils.Log("Error while reading rooms len " + err.Error())
return
}
utils.Log(roomsLen)
roomsBytes := make([]byte, roomsLen)
binary.Read(buffer, binary.LittleEndian, roomsBytes)
rooms := []*Room{}
err = json.Unmarshal(roomsBytes, rooms)
if err != nil {
utils.Log("Error while unmarshaling room list " + err.Error())
return
}
utils.Log(rooms)
utils.Log函数就是这个
func Log(msg interface{}) {
n := time.Now()
console.Call("log", fmt.Sprintf("[%v:%v:%v] %v", n.Hour(), n.Minute(), n.Second(), msg))
}
事情是第一个 uint64 是我正在等待的那个 (1) 但是第二个 uint64 我在登录时读取总是 0 (len(message)) 给我 52
并且由于它始终为 0,所以我无法正确解组它
所以我不确定我做错了什么,或者这是否不是使用 websockets 的正确方法。
小错误。您正在编写 uint64 但使用 "ReadUvarint" 来读取它(varint 是一种不同类型的数据)。
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
func main() {
b := bytes.NewBuffer([]byte{})
e1 := binary.Write(b, binary.LittleEndian, uint64(10))
e2 := binary.Write(b, binary.LittleEndian, uint64(20))
fmt.Println("writing 10 and 20", e1, e2)
{
r := bytes.NewBuffer(b.Bytes())
res, e := binary.ReadUvarint(r)
res2, e2 := binary.ReadUvarint(r)
fmt.Println("using readuvarint here :", res, res2, e, e2, b.Bytes())
}
{
r := bytes.NewBuffer(b.Bytes())
var res, res2 uint64
e := binary.Read(r, binary.LittleEndian, &res)
e2 := binary.Read(r, binary.LittleEndian, &res2)
fmt.Println("using read into uint64's here :", res, res2, e, e2, b.Bytes())
}
}
我目前正在尝试使用 websockets 进行通信,我的代码如下(我使用的是大猩猩)
buff := bytes.NewBuffer([]byte{})
binary.Write(buff, binary.LittleEndian, uint64(1))
binary.Write(buff, binary.LittleEndian, len(message))
binary.Write(buff, binary.LittleEndian, message)
client.Write <- buff.Bytes()
c.Write 通道在 for select 循环中
case msg := <-client.Write:
buffer := &bytes.Buffer{}
err := binary.Write(buffer, binary.LittleEndian, msg)
if err != nil {
return
}
err = client.Ws.WriteMessage(websocket.BinaryMessage, buffer.Bytes())
if err != nil {
return
}
buffer.Reset()
而客户端只是一个结构体
type Client struct {
Ws *websocket.Conn
Read chan []byte
Write chan []byte
Account *models.Account
}
消息发送成功,我是这样看的
b, err := conn.Read(buff)
if err != nil {
utils.Log("Error while reading from socket " + err.Error())
return
}
buffer := bytes.NewBuffer(buff[:b])
t, err := binary.ReadUvarint(buffer)
utils.Log(t)
if err != nil {
utils.Log("Error while reading from socket " + err.Error())
return
}
switch t {
case 1:
roomsLen, err := binary.ReadUvarint(buffer)
if err != nil {
utils.Log("Error while reading rooms len " + err.Error())
return
}
utils.Log(roomsLen)
roomsBytes := make([]byte, roomsLen)
binary.Read(buffer, binary.LittleEndian, roomsBytes)
rooms := []*Room{}
err = json.Unmarshal(roomsBytes, rooms)
if err != nil {
utils.Log("Error while unmarshaling room list " + err.Error())
return
}
utils.Log(rooms)
utils.Log函数就是这个
func Log(msg interface{}) {
n := time.Now()
console.Call("log", fmt.Sprintf("[%v:%v:%v] %v", n.Hour(), n.Minute(), n.Second(), msg))
}
事情是第一个 uint64 是我正在等待的那个 (1) 但是第二个 uint64 我在登录时读取总是 0 (len(message)) 给我 52
并且由于它始终为 0,所以我无法正确解组它
所以我不确定我做错了什么,或者这是否不是使用 websockets 的正确方法。
小错误。您正在编写 uint64 但使用 "ReadUvarint" 来读取它(varint 是一种不同类型的数据)。
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
func main() {
b := bytes.NewBuffer([]byte{})
e1 := binary.Write(b, binary.LittleEndian, uint64(10))
e2 := binary.Write(b, binary.LittleEndian, uint64(20))
fmt.Println("writing 10 and 20", e1, e2)
{
r := bytes.NewBuffer(b.Bytes())
res, e := binary.ReadUvarint(r)
res2, e2 := binary.ReadUvarint(r)
fmt.Println("using readuvarint here :", res, res2, e, e2, b.Bytes())
}
{
r := bytes.NewBuffer(b.Bytes())
var res, res2 uint64
e := binary.Read(r, binary.LittleEndian, &res)
e2 := binary.Read(r, binary.LittleEndian, &res2)
fmt.Println("using read into uint64's here :", res, res2, e, e2, b.Bytes())
}
}