forked from Terry-Mao/gopush2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
54 lines (44 loc) · 1.19 KB
/
client.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
package main
import (
"fmt"
"io"
"net/http"
)
func Client(w http.ResponseWriter, r *http.Request) {
html := fmt.Sprintf(`
<!doctype html>
<html>
<script type="text/javascript" src="http://img3.douban.com/js/packed_jquery.min6301986802.js" async="true"></script>
<script type="text/javascript">
var sock = null;
var wsuri = "ws://%s:%d/sub?key=Terry-Mao";
var mid = "0"
window.onload = function() {
try
{
sock = new WebSocket(wsuri);
}catch (e) {
alert(e.Message);
}
sock.onopen = function() {
alert("connected to " + wsuri);
}
sock.onerror = function(e) {
alert(" error from connect " + e.Message);
}
sock.onclose = function(e) {
alert("connection closed (" + e.code + ")");
}
sock.onmessage = function(e) {
alert("message received: " + e.data);
}
};
function send() {
sock.send(mid);
};
</script>
<h1>Push Service </h1>
<button onclick="send();">Sub</button>
`, Conf.Addr, Conf.Port)
io.WriteString(w, html)
}