/
main.go
79 lines (67 loc) · 1.81 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Try net/rpc (using encoding/gob) between backend and frontend (via GopherJS) through a websocket connection.
package main
import (
"fmt"
"io"
"net/http"
"net/rpc"
"github.com/shurcooL/go/gopherjs_http"
"golang.org/x/net/websocket"
)
type Args struct {
A, B int
}
type Arith struct{}
func (_ *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
fmt.Printf("locally multiplying %v by %v -> %v\n", args.A, args.B, *reply)
return nil
}
func main() {
rpc.Register(&Arith{})
http.Handle("/rpc-websocket", websocket.Handler(func(conn *websocket.Conn) {
// Why is this exported field undocumented?
//
// It seems it needs to be set to websocket.BinaryFrame so that
// the Write method sends bytes as binary rather than text frames.
conn.PayloadType = websocket.BinaryFrame
rpc.ServeConn(conn)
}))
http.HandleFunc("/index.html", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, `<html>
<head></head>
<body>
<pre id="output"></pre>
<script type="text/javascript" src="/script.js"></script>
</body>
</html>
`)
})
http.Handle("/script.js", gopherjs_http.GoFiles("./script.go"))
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
// ---
type dumpReadWriteCloser struct {
io.ReadWriteCloser
}
func (d dumpReadWriteCloser) Read(p []byte) (n int, err error) {
n, err = d.ReadWriteCloser.Read(p)
fmt.Println("Read:", n, err, p[:n])
fmt.Printf("%#q\n", string(p[:n]))
return
}
func (d dumpReadWriteCloser) Write(p []byte) (n int, err error) {
n, err = d.ReadWriteCloser.Write(p)
fmt.Println("Write:", n, err, p[:n])
fmt.Printf("%#q\n", string(p[:n]))
return
}
func (d dumpReadWriteCloser) Close() error {
err := d.ReadWriteCloser.Close()
fmt.Println("Close:", err)
return err
}