aboutsummaryrefslogtreecommitdiffstats
path: root/websocket/server.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-10-01 05:26:52 +0800
committerobscuren <geffobscura@gmail.com>2014-10-01 05:26:52 +0800
commit3af211dd65d6690afce9976a9f47ab1cdddb8d58 (patch)
tree2fbfade2d934d5c41a0e3bec449d1b23dea3b0ae /websocket/server.go
parentab6ede51d7fedb9270cab08ee732a834be34dab2 (diff)
downloadgo-tangerine-3af211dd65d6690afce9976a9f47ab1cdddb8d58.tar
go-tangerine-3af211dd65d6690afce9976a9f47ab1cdddb8d58.tar.gz
go-tangerine-3af211dd65d6690afce9976a9f47ab1cdddb8d58.tar.bz2
go-tangerine-3af211dd65d6690afce9976a9f47ab1cdddb8d58.tar.lz
go-tangerine-3af211dd65d6690afce9976a9f47ab1cdddb8d58.tar.xz
go-tangerine-3af211dd65d6690afce9976a9f47ab1cdddb8d58.tar.zst
go-tangerine-3af211dd65d6690afce9976a9f47ab1cdddb8d58.zip
Implemented WebSocket package
Diffstat (limited to 'websocket/server.go')
-rw-r--r--websocket/server.go127
1 files changed, 127 insertions, 0 deletions
diff --git a/websocket/server.go b/websocket/server.go
new file mode 100644
index 000000000..648f3fdc3
--- /dev/null
+++ b/websocket/server.go
@@ -0,0 +1,127 @@
+package websocket
+
+import (
+ "net/http"
+
+ "github.com/ethereum/eth-go/ethlog"
+
+ ws "code.google.com/p/go.net/websocket"
+)
+
+var logger = ethlog.NewLogger("WS")
+
+// Chat server.
+type Server struct {
+ httpServ string
+ pattern string
+ messages []*Message
+ clients map[int]*Client
+ addCh chan *Client
+ delCh chan *Client
+ sendAllCh chan string
+ doneCh chan bool
+ errCh chan error
+ msgFunc MsgFunc
+}
+
+// Create new chat server.
+func NewServer(pattern, httpServ string) *Server {
+ clients := make(map[int]*Client)
+ addCh := make(chan *Client)
+ delCh := make(chan *Client)
+ sendAllCh := make(chan string)
+ doneCh := make(chan bool)
+ errCh := make(chan error)
+
+ return &Server{
+ httpServ,
+ pattern,
+ nil,
+ clients,
+ addCh,
+ delCh,
+ sendAllCh,
+ doneCh,
+ errCh,
+ nil,
+ }
+}
+
+func (s *Server) Add(c *Client) {
+ s.addCh <- c
+}
+
+func (s *Server) Del(c *Client) {
+ s.delCh <- c
+}
+
+func (s *Server) SendAll(msg string) {
+ s.sendAllCh <- msg
+}
+
+func (s *Server) Done() {
+ s.doneCh <- true
+}
+
+func (s *Server) Err(err error) {
+ s.errCh <- err
+}
+
+func (s *Server) servHTTP() {
+ logger.Debugln("Serving http", s.httpServ)
+ err := http.ListenAndServe(s.httpServ, nil)
+
+ logger.Warnln(err)
+}
+
+func (s *Server) MessageFunc(f MsgFunc) {
+ s.msgFunc = f
+}
+
+// Listen and serve.
+// It serves client connection and broadcast request.
+func (s *Server) Listen() {
+ logger.Debugln("Listening server...")
+
+ // ws handler
+ onConnected := func(ws *ws.Conn) {
+ defer func() {
+ err := ws.Close()
+ if err != nil {
+ s.errCh <- err
+ }
+ }()
+
+ client := NewClient(ws, s)
+ client.onMessage = s.msgFunc
+ s.Add(client)
+ client.Listen()
+ }
+ // Disable Origin check. Request don't need to come necessarily from origin.
+ http.HandleFunc(s.pattern, func(w http.ResponseWriter, req *http.Request) {
+ s := ws.Server{Handler: ws.Handler(onConnected)}
+ s.ServeHTTP(w, req)
+ })
+ logger.Debugln("Created handler")
+
+ go s.servHTTP()
+
+ for {
+ select {
+
+ // Add new a client
+ case c := <-s.addCh:
+ s.clients[c.id] = c
+
+ // del a client
+ case c := <-s.delCh:
+ delete(s.clients, c.id)
+
+ case err := <-s.errCh:
+ logger.Debugln("Error:", err.Error())
+
+ case <-s.doneCh:
+ return
+ }
+ }
+}