-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
executable file
·86 lines (78 loc) · 1.86 KB
/
channel.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
80
81
82
83
84
85
86
package main
import (
"io"
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/channel"
)
func init() {
// build routing table
http.HandleFunc("/create_channel", handleCreateChannel)
http.HandleFunc("/listen", handleListen)
http.HandleFunc("/send", handleSend)
}
func handleSend(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
message := req.FormValue("message")
err := channel.SendJSON(ctx, "example", message)
if err != nil {
http.Error(res, err.Error(), 500)
}
io.WriteString(res, "Sent!")
}
func handleCreateChannel(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
tok, err := channel.Create(ctx, "example")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
io.WriteString(res, tok)
}
func handleListen(res http.ResponseWriter, req *http.Request) {
io.WriteString(res, `<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/_ah/channel/jsapi"></script>
</head>
<body>
<script>
// I want to listen on the channel
var token = prompt("Enter Your Token");
// listen
var channel = new goog.appengine.Channel(token);
// this:
var socket = channel.open();
socket.onopen = function(evt) {
console.log("OPEN", arguments);
};
socket.onmessage = function(msg) {
var data = JSON.parse(msg.data);
alert("Message Received: " + data);
};
socket.onerror = function(evt) {
console.log("ERROR", arguments);
};
socket.onclose = function(evt) {
console.log("CLOSE", arguments);
};
// or this:
var socket = channel.open({
onopen: function() {
console.log("OPEN", arguments);
},
onmessage: function(msg) {
var data = JSON.parse(msg.data);
alert("Message Received: " + data);
},
onerror: function() {
console.log("ERROR", arguments);
},
onclose: function() {
console.log("CLOSE", arguments);
}
});
</script>
</body>
</html>`)
}