|
| 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 <[email protected]> |
| 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. |
0 commit comments