I'm trying to use the protobuf with websocket, but I'm stuck on very simple
case like send and receive message, my code is sth like below:
const protoSpec = """
syntax = "proto3";
package DS;
enum TOPIC {
UNKNOWN = 0;
PARSER = 1;
CONNECTION = 2;
AUTH = 3;
EVENT = 4;
RECORD = 5;
RPC = 6;
PRESENCE = 7;
MONITORING = 8;
CLUSTER = 9;
LOCK = 10;
STATE_REGISTRY = 11;
ERROR = 100;
}
enum CONNECTION_ACTION {
UNKNOWN = 0;
ERROR = 1;
PING = 2;
PONG = 3;
ACCEPT = 4;
CHALLENGE = 5;
REJECT = 6;
REDIRECT = 7;
CLOSING = 8;
CLOSED = 9;
AUTHENTICATION_TIMEOUT = 100;
INVALID_MESSAGE = 101;
}
message Msg {
TOPIC topic = 1;
CONNECTION_ACTION action = 2;
string url = 3;
string protocolVersion = 5;
}
"""
parseProto(protoSpec)
var msg = new DS_Msg
msg.topic = DS_TOPIC.CONNECTION
msg.action = DS_CONNECTION_ACTION.CHALLENGE
msg.url = "ws://localhost:6020"
msg.protocolVersion = "0.1a"
var stream = newStringStream()
stream.write msg
let ws = waitFor newAsyncWebsocketClient("localhost", Port(6020), path="/")
echo "Connected!"
echo "Sending a challenge"
discard ws.sendBinary(stream.data) # is this correct?
proc ping() {.async.} =
while true:
await sleepAsync(6000)
echo "ping"
await ws.sendPing()
proc read() {.async.} =
while true:
let (opcode, data) = await ws.readData()
echo "(opcode: ", opcode, ", data: ", data, ")"
asyncCheck read()
asyncCheck ping()
runForever()
Run
My question are:
1. how to serialize the protocol encoded payload and send through websocket?
2. how to read and parse the protocol encoded payload from peer?