I am currently implementing an RPC interface (both client and server) about some proprietary product on top of a raw TCP connection. The documentation explicitly demands a JSON-RPC communication and it also adds the following tip:
In <doc number>, antenna and remote device are both server and client
In the interface, it appears that the client should be able to receive notifications
from the server, simple json message without the need to respond to them.
Basically, it looks like this:
Server ---------------------------------------------------- Client
<------------------------ Client Initiate Connection
Server acknowledge connection opened -------------->
Server sends a notification about some event------->
Server sends another notif. about another event---->
<---------------------------- Client Stop Connection
Server acknowledge connection stopped ------------->
Doing a simple request/response event is easy:
The server looks like this:
func (l *Listener) GetLine(line []byte, ack *bool) error {
fmt.Println(string(line))
return nil
}
func main() {
addy, err := net.ResolveTCPAddr("tcp", "0.0.0.0:42586")
inbound, _:= net.ListenTCP("tcp", addy)
listener := new(Listener)
rpc.Register(listener)
rpc.Accept(inbound)
}
And the client is able to call to the server function
client, err := rpc.Dial("tcp", "localhost:42586")
in := bufio.NewReader(os.Stdin)
for {
line, _, err := in.ReadLine()
var reply bool
client.Call("Listener.GetLine", line, &reply)
}
In this situation, I do not see how I can send data from the server to the client. How can I do this using the same TCP connection? How can they be both server and client ?