-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
45 lines (41 loc) · 945 Bytes
/
server.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
package main
import (
"log"
"net/http"
"net/rpc"
"time"
"github.com/dustywilson/gopherjs-rpc/shared"
"github.com/nytimes/gziphandler"
"golang.org/x/net/websocket"
)
func main() {
http.Handle("/ws-rpc", websocket.Handler(func(conn *websocket.Conn) {
conn.PayloadType = websocket.BinaryFrame
r := rpc.NewClient(conn)
chatter(r)
}))
http.Handle("/", gziphandler.GzipHandler(http.FileServer(http.Dir("www"))))
panic(http.ListenAndServe(":5454", nil))
}
func chatter(r *rpc.Client) {
for {
output := shared.ChatMessage{
Name: "Server",
Time: time.Now(),
Message: "Something important.",
}
log.Println("Output: ", output)
var input shared.ChatMessage
err := r.Call("RPC.Message", &output, &input)
if err != nil {
log.Println("ERROR: ", err)
if err == rpc.ErrShutdown {
log.Println("... exiting!")
return
}
} else {
log.Println("Input: ", input)
}
time.Sleep(time.Second)
}
}