Skip to content

Commit c118319

Browse files
committed
Split into multiple files. Misc improvements.
1 parent 64eb3e2 commit c118319

File tree

5 files changed

+184
-172
lines changed

5 files changed

+184
-172
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ include $(GOROOT)/src/Make.inc
66
TARG=example
77
GOFILES=\
88
main.go\
9+
hub.go\
10+
conn.go
911

1012
include $(GOROOT)/src/Make.cmd

conn.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"websocket"
5+
)
6+
7+
type connection struct {
8+
// The websocket connection.
9+
ws *websocket.Conn
10+
11+
// Buffered channel of outbound messages.
12+
send chan string
13+
}
14+
15+
func (c *connection) reader() {
16+
for {
17+
var message string
18+
err := websocket.Message.Receive(c.ws, &message)
19+
if err != nil {
20+
break
21+
}
22+
h.broadcast <- message
23+
}
24+
c.ws.Close()
25+
}
26+
27+
func (c *connection) writer() {
28+
for message := range c.send {
29+
err := websocket.Message.Send(c.ws, message)
30+
if err != nil {
31+
break
32+
}
33+
}
34+
c.ws.Close()
35+
}
36+
37+
func wsHandler(ws *websocket.Conn) {
38+
c := &connection{send: make(chan string, 256), ws: ws}
39+
h.register <- c
40+
defer func() { h.unregister <- c }()
41+
go c.writer()
42+
c.reader()
43+
}

home.html

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<html>
2+
<head>
3+
<title>Chat Example</title>
4+
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
5+
<script type="text/javascript">
6+
$(function() {
7+
8+
var conn;
9+
var msg = $("#msg");
10+
var log = $("#log");
11+
12+
function appendLog(msg) {
13+
var d = log[0]
14+
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
15+
msg.appendTo(log)
16+
if (doScroll) {
17+
d.scrollTop = d.scrollHeight - d.clientHeight;
18+
}
19+
}
20+
21+
$("#form").submit(function() {
22+
if (!conn) {
23+
return false;
24+
}
25+
if (!msg.val()) {
26+
return false;
27+
}
28+
conn.send(msg.val());
29+
msg.val("");
30+
return false
31+
});
32+
33+
if (window["WebSocket"]) {
34+
conn = new WebSocket("ws://{{$}}/ws");
35+
conn.onclose = function(evt) {
36+
appendLog($("<div><b>Connection closed.</b></div>"))
37+
}
38+
conn.onmessage = function(evt) {
39+
appendLog($("<div/>").text(evt.data))
40+
}
41+
} else {
42+
appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
43+
}
44+
});
45+
</script>
46+
<style type="text/css">
47+
html {
48+
overflow: hidden;
49+
}
50+
51+
body {
52+
overflow: hidden;
53+
padding: 0;
54+
margin: 0;
55+
width: 100%;
56+
height: 100%;
57+
background: gray;
58+
}
59+
60+
#log {
61+
background: white;
62+
margin: 0;
63+
padding: 0.5em 0.5em 0.5em 0.5em;
64+
position: absolute;
65+
top: 0.5em;
66+
left: 0.5em;
67+
right: 0.5em;
68+
bottom: 3em;
69+
overflow: auto;
70+
}
71+
72+
#form {
73+
padding: 0 0.5em 0 0.5em;
74+
margin: 0;
75+
position: absolute;
76+
bottom: 1em;
77+
left: 0px;
78+
width: 100%;
79+
overflow: hidden;
80+
}
81+
82+
</style>
83+
</head>
84+
<body>
85+
<div id="log"></div>
86+
<form id="form">
87+
<input type="submit" value="Send" />
88+
<input type="text" id="msg" size="64"/>
89+
</form>
90+
</body>
91+
</html>

hub.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
type hub struct {
4+
// Registered connections.
5+
connections map[*connection]bool
6+
7+
// Inbound messages from the connections.
8+
broadcast chan string
9+
10+
// Register requests from the connections.
11+
register chan *connection
12+
13+
// Unregister requests from connections.
14+
unregister chan *connection
15+
}
16+
17+
var h = hub{
18+
broadcast: make(chan string),
19+
register: make(chan *connection),
20+
unregister: make(chan *connection),
21+
connections: make(map[*connection]bool),
22+
}
23+
24+
func (h *hub) run() {
25+
for {
26+
select {
27+
case c := <-h.register:
28+
h.connections[c] = true
29+
case c := <-h.unregister:
30+
delete(h.connections, c)
31+
close(c.send)
32+
case m := <-h.broadcast:
33+
for c := range h.connections {
34+
select {
35+
case c.send <- m:
36+
default:
37+
delete(h.connections, c)
38+
close(c.send)
39+
go c.ws.Close()
40+
}
41+
}
42+
}
43+
}
44+
}

main.go

Lines changed: 4 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,12 @@ import (
88
"websocket"
99
)
1010

11-
type message struct {
12-
text string
13-
}
14-
15-
type client struct {
16-
messages chan message
17-
ws *websocket.Conn
18-
}
19-
20-
type hub struct {
21-
messages chan message
22-
subscribes chan *client
23-
unsubscribes chan *client
24-
}
25-
26-
func (h *hub) run() {
27-
clients := make(map[*client]bool)
28-
for {
29-
select {
30-
case c := <-h.subscribes:
31-
clients[c] = true
32-
case c := <-h.unsubscribes:
33-
if clients[c] {
34-
delete(clients, c)
35-
close(c.messages)
36-
}
37-
case m := <-h.messages:
38-
for c := range clients {
39-
select {
40-
case c.messages <- m:
41-
default:
42-
delete(clients, c)
43-
close(c.messages)
44-
go c.ws.Close()
45-
}
46-
}
47-
}
48-
}
49-
}
50-
51-
func (c *client) shutdown() {
52-
h.unsubscribes <- c
53-
c.ws.Close()
54-
}
55-
56-
func (c *client) reader() {
57-
defer c.shutdown()
58-
for {
59-
var text string
60-
err := websocket.Message.Receive(c.ws, &text)
61-
if err != nil {
62-
break
63-
}
64-
h.messages <- message{text}
65-
}
66-
}
67-
68-
func (c *client) writer() {
69-
defer c.shutdown()
70-
for m := range c.messages {
71-
err := websocket.Message.Send(c.ws, m.text)
72-
if err != nil {
73-
break
74-
}
75-
}
76-
}
77-
7811
var addr = flag.String("addr", ":8080", "http service address")
12+
var homeTempl = template.Must(template.ParseFiles("home.html"))
7913

80-
var h = hub{make(chan message), make(chan *client), make(chan *client)}
14+
func homeHandler(c http.ResponseWriter, req *http.Request) {
15+
homeTempl.Execute(c, req.Host)
16+
}
8117

8218
func main() {
8319
flag.Parse()
@@ -88,107 +24,3 @@ func main() {
8824
log.Fatal("ListenAndServe:", err)
8925
}
9026
}
91-
92-
func wsHandler(ws *websocket.Conn) {
93-
c := &client{make(chan message, 256), ws}
94-
h.subscribes <- c
95-
go c.writer()
96-
c.reader()
97-
}
98-
99-
func homeHandler(c http.ResponseWriter, req *http.Request) {
100-
homeTempl.Execute(c, req.Host)
101-
}
102-
103-
var homeTempl = template.Must(template.New("home").Parse(`
104-
<html>
105-
<head>
106-
<title>Chat Example</title>
107-
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
108-
<script type="text/javascript">
109-
$(function() {
110-
111-
var conn;
112-
var msg = $("#msg");
113-
var log = $("#log");
114-
115-
function appendLog(msg) {
116-
var d = log[0]
117-
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
118-
msg.appendTo(log)
119-
if (doScroll) {
120-
d.scrollTop = d.scrollHeight - d.clientHeight;
121-
}
122-
}
123-
124-
$("#form").submit(function() {
125-
if (!conn) {
126-
return false;
127-
}
128-
if (!msg.val()) {
129-
return false;
130-
}
131-
conn.send(msg.val());
132-
msg.val("");
133-
return false
134-
});
135-
136-
if (window["WebSocket"]) {
137-
conn = new WebSocket("ws://{{$}}/ws");
138-
conn.onclose = function(evt) {
139-
appendLog($("<div><b>Connection closed.</b></div>"))
140-
}
141-
conn.onmessage = function(evt) {
142-
appendLog($("<div/>").text(evt.data))
143-
}
144-
} else {
145-
appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
146-
}
147-
});
148-
</script>
149-
<style type="text/css">
150-
html {
151-
overflow: hidden;
152-
}
153-
154-
body {
155-
overflow: hidden;
156-
padding: 0;
157-
margin: 0;
158-
width: 100%;
159-
height: 100%;
160-
background: gray;
161-
}
162-
163-
#log {
164-
background: white;
165-
margin: 0;
166-
padding: 0.5em 0.5em 0.5em 0.5em;
167-
position: absolute;
168-
top: 0.5em;
169-
left: 0.5em;
170-
right: 0.5em;
171-
bottom: 3em;
172-
overflow: auto;
173-
}
174-
175-
#form {
176-
padding: 0 0.5em 0 0.5em;
177-
margin: 0;
178-
position: absolute;
179-
bottom: 1em;
180-
left: 0px;
181-
width: 100%;
182-
overflow: hidden;
183-
}
184-
185-
</style>
186-
</head>
187-
<body>
188-
<div id="log"></div>
189-
<form id="form">
190-
<input type="submit" value="Send" />
191-
<input type="text" id="msg" size="64"/>
192-
</form>
193-
</body>
194-
</html> `))

0 commit comments

Comments
 (0)