/
main.go
50 lines (41 loc) · 888 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"log"
"net/http"
"net/rpc"
"net/rpc/jsonrpc"
"github.com/gorilla/websocket"
"github.com/mem/ws-rpc/internal/common"
)
func main() {
ws, res, err := dialer.Dial("ws://127.0.0.1:8010/ws", http.Header{})
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
defer ws.Close()
log.Println(res)
handle(ws)
}
var dialer = websocket.Dialer{
ReadBufferSize: common.MaxMessageSize,
WriteBufferSize: common.MaxMessageSize,
}
func handle(ws *websocket.Conn) {
defer func() {
ws.Close()
}()
rwc := &common.ReadWriteCloser{WS: ws}
codec := jsonrpc.NewClientCodec(rwc)
c := rpc.NewClientWithCodec(codec)
// c := rpc.NewClient(rwc)
for {
args := &common.HelloArgs{Msg: "Hello, World"}
var reply common.HelloReply
err := c.Call("Comm.Hello", args, &reply)
if err != nil {
log.Printf("%v", err)
break
}
log.Printf("%v", reply)
}
}