forked from madari/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eba389d
Showing
20 changed files
with
1,548 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
example/example | ||
*.o | ||
*.a | ||
*.[568vq] | ||
[568vq].out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "example/www/vendor/socket.io-client"] | ||
path = example/www/vendor/socket.io-client | ||
url = http://github.com/LearnBoost/Socket.IO.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) 2010 Jukka-Pekka Kekkonen <karatepekka@gmail.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the 'Software'), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY , | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
all: socketio | ||
|
||
socketio: | ||
make -C src | ||
|
||
clean: | ||
make -C src clean | ||
|
||
test: | ||
make -C src test | ||
|
||
install: | ||
make -C src install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
go-socket.io | ||
============ | ||
|
||
The `socketio` package is a simple abstraction layer for different web browser- | ||
supported transport mechanisms. It is meant to be fully compatible with the | ||
[Socket.IO client](http://github.com/LearnBoost/Socket.IO) JavaScript-library by | ||
[LearnBoost Labs](http://socket.io/), but through custom formatters it should | ||
suit for any client implementation. | ||
|
||
It provides an easy way for developers to rapidly prototype with the most | ||
popular browser transport mechanism today: | ||
|
||
- [HTML5 WebSockets](http://dev.w3.org/html5/websockets/) | ||
- [XHR Polling](http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest_long_polling) | ||
- [XHR Multipart Streaming](http://en.wikipedia.org/wiki/Comet_%28programming%29#XMLHttpRequest) | ||
|
||
## Disclaimer | ||
|
||
**The go-socket.io is still very experimental, and you should consider it as an | ||
early prototype.** I hope it will generate some conversation and most | ||
importantly get other contributors. | ||
|
||
## Crash course | ||
|
||
The `socketio` package works hand-in-hand with the standard `http` package (by | ||
plugging itself into a configurable `http.ServeMux`) and hence it doesn't need a | ||
full network port for itself. It has an callback-style event handling API. The | ||
callbacks are: | ||
|
||
- *socketio.OnConnect* | ||
- *socketio.OnDisconnect* | ||
- *socketio.OnMessage* | ||
|
||
Other utility-methods include: | ||
|
||
- *socketio.Mux* | ||
- *socketio.Broadcast* | ||
- *socketio.BroadcastExcept* | ||
- *socketio.IterConns* | ||
- *socketio.GetConn* | ||
|
||
Each new connection will be automatically assigned an unique session id and | ||
using those the clients can reconnect without losing messages: the socketio | ||
package persists client's pending messages (until some configurable point) if | ||
they can't be immediately delivered. All writes through the API are by design | ||
asynchronous. For critical messages the package provides a way to detect | ||
succesful deliveries by using `socketio.Conn.WaitFlush` *[TODO: better | ||
solution]*. All-in-all, the `socketio.Conn` type has two methods to handle | ||
message passing: | ||
|
||
- *socketio.Conn.Send* | ||
- *socketio.Conn.WaitFlush* | ||
|
||
## Example: A simple chat server | ||
|
||
package main | ||
|
||
import ( | ||
"http" | ||
"log" | ||
"socketio" | ||
) | ||
|
||
// A very simple chat server | ||
func main() { | ||
// create the server and mux it to /socket.io/ in http.DefaultServeMux | ||
sio := socketio.NewSocketIO(nil) | ||
sio.Mux("/socket.io/", nil) | ||
|
||
// serve static files under www/ | ||
http.Handle("/", http.FileServer("www/", "/")) | ||
|
||
// client connected. Let everyone know about this. | ||
sio.OnConnect(func(c *socketio.Conn) { | ||
// socketio does not care what you are sending as long as it is | ||
// marshallable by the standard json-package | ||
sio.Broadcast(struct{ announcement string }{"connected: " + c.String()}) | ||
}) | ||
|
||
// client disconnected. Let the other users know about this. | ||
sio.OnDisconnect(func(c *socketio.Conn) { | ||
sio.BroadcastExcept(c, | ||
struct{ announcement string }{"disconnected: " + c.String()}) | ||
}) | ||
|
||
// client sent a message. Let's broadcast it to the other users. | ||
sio.OnMessage(func(c *socketio.Conn, msg string) { | ||
sio.BroadcastExcept(c, | ||
struct{ message []string }{[]string{c.String(), msg}}) | ||
}) | ||
|
||
// start serving | ||
log.Stdout("Server started.") | ||
if err := http.ListenAndServe(":8080", nil); err != nil { | ||
log.Stdout("ListenAndServe: %s", err.String()) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
## License | ||
|
||
(The MIT License) | ||
|
||
Copyright (c) 2010 Jukka-Pekka Kekkonen <karatepekka@gmail.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
include $(GOROOT)/src/Make.$(GOARCH) | ||
|
||
SIO = ../src | ||
GC += -I$(SIO)/_obj/ | ||
LD += -L$(SIO)/_obj/ | ||
LIB = $(SIO)/_obj/socketio.a | ||
TARG = example | ||
GOFILES = example.go | ||
|
||
$(TARG): $(LIB) | ||
|
||
$(LIB): $(wildcard $(SIO)/*.go) | ||
make -C $(SIO) | ||
|
||
include $(GOROOT)/src/Make.cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"container/vector" | ||
"http" | ||
"log" | ||
"os" | ||
"socketio" | ||
"sync" | ||
) | ||
|
||
// A very simple chat server | ||
func main() { | ||
buffer := new(vector.Vector) | ||
mutex := new(sync.Mutex) | ||
|
||
// Create the socket.io server and mux it to /socket.io/ | ||
sio := socketio.NewSocketIO(nil) | ||
sio.Mux("/socket.io/", nil) | ||
|
||
// Server static files under www/ | ||
http.Handle("/", http.FileServer("www/", "/")) | ||
|
||
// Serve | ||
defer func() { | ||
log.Stdout("Server started. Tune your browser to http://localhost:8080/") | ||
if err := http.ListenAndServe(":8080", nil); err != nil { | ||
log.Stdout("ListenAndServe: %s", err.String()) | ||
os.Exit(1) | ||
} | ||
}() | ||
|
||
|
||
//// SOCKET.IO EVENT HANDLERS //// | ||
|
||
// Client connected | ||
// Send the buffer to the client and broadcast announcement | ||
sio.OnConnect(func(c *socketio.Conn) { | ||
mutex.Lock() | ||
c.Send(struct{ buffer []interface{} }{buffer.Data()}) | ||
mutex.Unlock() | ||
|
||
sio.Broadcast(struct{ announcement string }{"connected: " + c.String()}) | ||
}) | ||
|
||
// Client disconnected | ||
// Send the announcement | ||
sio.OnDisconnect(func(c *socketio.Conn) { | ||
sio.Broadcast(struct{ announcement string }{"disconnected: " + c.String()}) | ||
}) | ||
|
||
// Client sent a message | ||
// Store it to the buffer and broadcast it | ||
sio.OnMessage(func(c *socketio.Conn, msg string) { | ||
payload := struct{ message []string }{[]string{c.String(), msg}} | ||
|
||
mutex.Lock() | ||
buffer.Push(payload) | ||
mutex.Unlock() | ||
|
||
sio.BroadcastExcept(c, payload) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<!doctype html> | ||
<!-- | ||
Original chat client from: | ||
http://github.com/LearnBoost/Socket.IO-node/blob/master/test/chat.html | ||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
(The MIT License) | ||
Copyright (c) 2010 LearnBoost <dev@learnboost.com> | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
--> | ||
<html> | ||
<head> | ||
<title>socket.io client test</title> | ||
<script src="/vendor/socket.io-client/socket.io.js"></script> | ||
</head> | ||
<body> | ||
<script type="text/javascript"> | ||
io.setPath('/vendor/socket.io-client/'); | ||
|
||
function message(obj){ | ||
var el = document.createElement('p'); | ||
if ('announcement' in obj) { | ||
el.innerHTML = '<em>' + obj.announcement; | ||
} else if ('message' in obj) { | ||
el.innerHTML = '<b>' + obj.message[0] + ':</b> ' + obj.message[1]; | ||
} | ||
document.getElementById('chat').appendChild(el); | ||
document.getElementById('chat').scrollTop = 1000000; | ||
} | ||
|
||
function send(){ | ||
var val = document.getElementById('text').value; | ||
socket.send(val); | ||
message({ message: ['you', val] }); | ||
document.getElementById('text').value = ''; | ||
} | ||
|
||
var socket = new io.Socket('localhost', { | ||
rememberTransport: false, | ||
port: 8080, | ||
transports: ['websocket', 'xhr-multipart', 'xhr-polling'] | ||
}); | ||
socket.connect(); | ||
socket.addEvent('message', function(data) { | ||
if ('buffer' in data) { | ||
document.getElementById('form').style.display = 'block'; | ||
document.getElementById('chat').innerHTML = ''; | ||
for (var i in data.buffer) { | ||
message(data.buffer[i]); | ||
} | ||
} else { | ||
message(data); | ||
} | ||
}); | ||
</script> | ||
|
||
<h1>Sample chat client</h1> | ||
<div id="chat"><p>Connecting...</p></div> | ||
<form id="form" onsubmit="send(); return false"> | ||
<input type="text" autocomplete="off" id="text" /><input type="submit" value="Send" /> | ||
</form> | ||
|
||
<style type="text/css"> | ||
#chat { height: 300px; overflow: auto; width: 800px; border: 1px solid #eee; font: 13px Helvetica, Arial; } | ||
#chat p { padding: 8px; margin: 0; } | ||
#chat p:nth-child(odd) { background: #F6F6F6; } | ||
#form { width: 782px; background: #333; padding: 5px 10px; display: none; } | ||
#form input[type=text] { width: 700px; padding: 5px; background: #fff; border: 1px solid #fff; } | ||
#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; } | ||
#form input[type=submit]:hover { background: #A2A2A2; } | ||
#form input[type=submit]:active { position: relative; top: 2px; } | ||
</style> | ||
</body> | ||
</html> |
Submodule socket.io-client
added at
8a34dc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
include $(GOROOT)/src/Make.$(GOARCH) | ||
|
||
TARG = socketio | ||
GOFILES = \ | ||
util.go \ | ||
broadcaster.go \ | ||
socketio.go \ | ||
connection.go \ | ||
formatter.go \ | ||
transport.go \ | ||
transport_xhrpolling.go \ | ||
transport_xhrmultipart.go \ | ||
transport_websocket.go | ||
|
||
|
||
include $(GOROOT)/src/Make.pkg |
Oops, something went wrong.