Skip to content

Commit eba389d

Browse files
committed
initial import
0 parents  commit eba389d

20 files changed

+1548
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
example/example
2+
*.o
3+
*.a
4+
*.[568vq]
5+
[568vq].out

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "example/www/vendor/socket.io-client"]
2+
path = example/www/vendor/socket.io-client
3+
url = http://github.com/LearnBoost/Socket.IO.git

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2010 Jukka-Pekka Kekkonen <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the 'Software'), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
all: socketio
2+
3+
socketio:
4+
make -C src
5+
6+
clean:
7+
make -C src clean
8+
9+
test:
10+
make -C src test
11+
12+
install:
13+
make -C src install

README.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
go-socket.io
2+
============
3+
4+
The `socketio` package is a simple abstraction layer for different web browser-
5+
supported transport mechanisms. It is meant to be fully compatible with the
6+
[Socket.IO client](http://github.com/LearnBoost/Socket.IO) JavaScript-library by
7+
[LearnBoost Labs](http://socket.io/), but through custom formatters it should
8+
suit for any client implementation.
9+
10+
It provides an easy way for developers to rapidly prototype with the most
11+
popular browser transport mechanism today:
12+
13+
- [HTML5 WebSockets](http://dev.w3.org/html5/websockets/)
14+
- [XHR Polling](http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest_long_polling)
15+
- [XHR Multipart Streaming](http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest)
16+
17+
## Disclaimer
18+
19+
**The go-socket.io is still very experimental, and you should consider it as an
20+
early prototype.** I hope it will generate some conversation and most
21+
importantly get other contributors.
22+
23+
## Crash course
24+
25+
The `socketio` package works hand-in-hand with the standard `http` package (by
26+
plugging itself into a configurable `http.ServeMux`) and hence it doesn't need a
27+
full network port for itself. It has an callback-style event handling API. The
28+
callbacks are:
29+
30+
- *socketio.OnConnect*
31+
- *socketio.OnDisconnect*
32+
- *socketio.OnMessage*
33+
34+
Other utility-methods include:
35+
36+
- *socketio.Mux*
37+
- *socketio.Broadcast*
38+
- *socketio.BroadcastExcept*
39+
- *socketio.IterConns*
40+
- *socketio.GetConn*
41+
42+
Each new connection will be automatically assigned an unique session id and
43+
using those the clients can reconnect without losing messages: the socketio
44+
package persists client's pending messages (until some configurable point) if
45+
they can't be immediately delivered. All writes through the API are by design
46+
asynchronous. For critical messages the package provides a way to detect
47+
succesful deliveries by using `socketio.Conn.WaitFlush` *[TODO: better
48+
solution]*. All-in-all, the `socketio.Conn` type has two methods to handle
49+
message passing:
50+
51+
- *socketio.Conn.Send*
52+
- *socketio.Conn.WaitFlush*
53+
54+
## Example: A simple chat server
55+
56+
package main
57+
58+
import (
59+
"http"
60+
"log"
61+
"socketio"
62+
)
63+
64+
// A very simple chat server
65+
func main() {
66+
// create the server and mux it to /socket.io/ in http.DefaultServeMux
67+
sio := socketio.NewSocketIO(nil)
68+
sio.Mux("/socket.io/", nil)
69+
70+
// serve static files under www/
71+
http.Handle("/", http.FileServer("www/", "/"))
72+
73+
// client connected. Let everyone know about this.
74+
sio.OnConnect(func(c *socketio.Conn) {
75+
// socketio does not care what you are sending as long as it is
76+
// marshallable by the standard json-package
77+
sio.Broadcast(struct{ announcement string }{"connected: " + c.String()})
78+
})
79+
80+
// client disconnected. Let the other users know about this.
81+
sio.OnDisconnect(func(c *socketio.Conn) {
82+
sio.BroadcastExcept(c,
83+
struct{ announcement string }{"disconnected: " + c.String()})
84+
})
85+
86+
// client sent a message. Let's broadcast it to the other users.
87+
sio.OnMessage(func(c *socketio.Conn, msg string) {
88+
sio.BroadcastExcept(c,
89+
struct{ message []string }{[]string{c.String(), msg}})
90+
})
91+
92+
// start serving
93+
log.Stdout("Server started.")
94+
if err := http.ListenAndServe(":8080", nil); err != nil {
95+
log.Stdout("ListenAndServe: %s", err.String())
96+
os.Exit(1)
97+
}
98+
}
99+
100+
## License
101+
102+
(The MIT License)
103+
104+
Copyright (c) 2010 Jukka-Pekka Kekkonen &lt;[email protected]&gt;
105+
106+
Permission is hereby granted, free of charge, to any person obtaining
107+
a copy of this software and associated documentation files (the
108+
'Software'), to deal in the Software without restriction, including
109+
without limitation the rights to use, copy, modify, merge, publish,
110+
distribute, sublicense, and/or sell copies of the Software, and to
111+
permit persons to whom the Software is furnished to do so, subject to
112+
the following conditions:
113+
114+
The above copyright notice and this permission notice shall be
115+
included in all copies or substantial portions of the Software.
116+
117+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
118+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
119+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
120+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
121+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
122+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
123+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

example/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include $(GOROOT)/src/Make.$(GOARCH)
2+
3+
SIO = ../src
4+
GC += -I$(SIO)/_obj/
5+
LD += -L$(SIO)/_obj/
6+
LIB = $(SIO)/_obj/socketio.a
7+
TARG = example
8+
GOFILES = example.go
9+
10+
$(TARG): $(LIB)
11+
12+
$(LIB): $(wildcard $(SIO)/*.go)
13+
make -C $(SIO)
14+
15+
include $(GOROOT)/src/Make.cmd

example/example.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"container/vector"
5+
"http"
6+
"log"
7+
"os"
8+
"socketio"
9+
"sync"
10+
)
11+
12+
// A very simple chat server
13+
func main() {
14+
buffer := new(vector.Vector)
15+
mutex := new(sync.Mutex)
16+
17+
// Create the socket.io server and mux it to /socket.io/
18+
sio := socketio.NewSocketIO(nil)
19+
sio.Mux("/socket.io/", nil)
20+
21+
// Server static files under www/
22+
http.Handle("/", http.FileServer("www/", "/"))
23+
24+
// Serve
25+
defer func() {
26+
log.Stdout("Server started. Tune your browser to http://localhost:8080/")
27+
if err := http.ListenAndServe(":8080", nil); err != nil {
28+
log.Stdout("ListenAndServe: %s", err.String())
29+
os.Exit(1)
30+
}
31+
}()
32+
33+
34+
//// SOCKET.IO EVENT HANDLERS ////
35+
36+
// Client connected
37+
// Send the buffer to the client and broadcast announcement
38+
sio.OnConnect(func(c *socketio.Conn) {
39+
mutex.Lock()
40+
c.Send(struct{ buffer []interface{} }{buffer.Data()})
41+
mutex.Unlock()
42+
43+
sio.Broadcast(struct{ announcement string }{"connected: " + c.String()})
44+
})
45+
46+
// Client disconnected
47+
// Send the announcement
48+
sio.OnDisconnect(func(c *socketio.Conn) {
49+
sio.Broadcast(struct{ announcement string }{"disconnected: " + c.String()})
50+
})
51+
52+
// Client sent a message
53+
// Store it to the buffer and broadcast it
54+
sio.OnMessage(func(c *socketio.Conn, msg string) {
55+
payload := struct{ message []string }{[]string{c.String(), msg}}
56+
57+
mutex.Lock()
58+
buffer.Push(payload)
59+
mutex.Unlock()
60+
61+
sio.BroadcastExcept(c, payload)
62+
})
63+
}

example/www/index.html

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!doctype html>
2+
<!--
3+
4+
Original chat client from:
5+
http://github.com/LearnBoost/Socket.IO-node/blob/master/test/chat.html
6+
7+
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
9+
(The MIT License)
10+
11+
Copyright (c) 2010 LearnBoost <[email protected]>
12+
13+
Permission is hereby granted, free of charge, to any person obtaining
14+
a copy of this software and associated documentation files (the
15+
'Software'), to deal in the Software without restriction, including
16+
without limitation the rights to use, copy, modify, merge, publish,
17+
distribute, sublicense, and/or sell copies of the Software, and to
18+
permit persons to whom the Software is furnished to do so, subject to
19+
the following conditions:
20+
21+
The above copyright notice and this permission notice shall be
22+
included in all copies or substantial portions of the Software.
23+
24+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31+
32+
-->
33+
<html>
34+
<head>
35+
<title>socket.io client test</title>
36+
<script src="/vendor/socket.io-client/socket.io.js"></script>
37+
</head>
38+
<body>
39+
<script type="text/javascript">
40+
io.setPath('/vendor/socket.io-client/');
41+
42+
function message(obj){
43+
var el = document.createElement('p');
44+
if ('announcement' in obj) {
45+
el.innerHTML = '<em>' + obj.announcement;
46+
} else if ('message' in obj) {
47+
el.innerHTML = '<b>' + obj.message[0] + ':</b> ' + obj.message[1];
48+
}
49+
document.getElementById('chat').appendChild(el);
50+
document.getElementById('chat').scrollTop = 1000000;
51+
}
52+
53+
function send(){
54+
var val = document.getElementById('text').value;
55+
socket.send(val);
56+
message({ message: ['you', val] });
57+
document.getElementById('text').value = '';
58+
}
59+
60+
var socket = new io.Socket('localhost', {
61+
rememberTransport: false,
62+
port: 8080,
63+
transports: ['websocket', 'xhr-multipart', 'xhr-polling']
64+
});
65+
socket.connect();
66+
socket.addEvent('message', function(data) {
67+
if ('buffer' in data) {
68+
document.getElementById('form').style.display = 'block';
69+
document.getElementById('chat').innerHTML = '';
70+
for (var i in data.buffer) {
71+
message(data.buffer[i]);
72+
}
73+
} else {
74+
message(data);
75+
}
76+
});
77+
</script>
78+
79+
<h1>Sample chat client</h1>
80+
<div id="chat"><p>Connecting...</p></div>
81+
<form id="form" onsubmit="send(); return false">
82+
<input type="text" autocomplete="off" id="text" /><input type="submit" value="Send" />
83+
</form>
84+
85+
<style type="text/css">
86+
#chat { height: 300px; overflow: auto; width: 800px; border: 1px solid #eee; font: 13px Helvetica, Arial; }
87+
#chat p { padding: 8px; margin: 0; }
88+
#chat p:nth-child(odd) { background: #F6F6F6; }
89+
#form { width: 782px; background: #333; padding: 5px 10px; display: none; }
90+
#form input[type=text] { width: 700px; padding: 5px; background: #fff; border: 1px solid #fff; }
91+
#form input[type=submit] { cursor: pointer; background: #999; border: none; padding: 6px 8px; -moz-border-radius: 8px; -webkit-border-radius: 8px; margin-left: 5px; text-shadow: 0 1px 0 #fff; }
92+
#form input[type=submit]:hover { background: #A2A2A2; }
93+
#form input[type=submit]:active { position: relative; top: 2px; }
94+
</style>
95+
</body>
96+
</html>

example/www/vendor/socket.io-client

Submodule socket.io-client added at 8a34dc7

src/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include $(GOROOT)/src/Make.$(GOARCH)
2+
3+
TARG = socketio
4+
GOFILES = \
5+
util.go \
6+
broadcaster.go \
7+
socketio.go \
8+
connection.go \
9+
formatter.go \
10+
transport.go \
11+
transport_xhrpolling.go \
12+
transport_xhrmultipart.go \
13+
transport_websocket.go
14+
15+
16+
include $(GOROOT)/src/Make.pkg

0 commit comments

Comments
 (0)