Author: echatellier Date: 2013-07-26 22:04:45 +0200 (Fri, 26 Jul 2013) New Revision: 2916 Url: http://chorem.org/projects/jtimer/repository/revisions/2916 Log: Add go websocket server and js client Added: branches/ng-jtimer/jtimer-server/src/main/go/conn.go branches/ng-jtimer/jtimer-server/src/main/go/hub.go branches/ng-jtimer/jtimer-server/src/main/go/readme.txt Modified: branches/ng-jtimer/jtimer-server/src/main/go/jtimer.go branches/ng-jtimer/jtimer-server/src/main/webapp/js/controllers.js Added: branches/ng-jtimer/jtimer-server/src/main/go/conn.go =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/conn.go (rev 0) +++ branches/ng-jtimer/jtimer-server/src/main/go/conn.go 2013-07-26 20:04:45 UTC (rev 2916) @@ -0,0 +1,43 @@ +package main + +import ( + "code.google.com/p/go.net/websocket" +) + +type connection struct { + // The websocket connection. + ws *websocket.Conn + + // Buffered channel of outbound messages. + send chan string +} + +func (c *connection) reader() { + for { + var message string + err := websocket.Message.Receive(c.ws, &message) + if err != nil { + break + } + h.broadcast <- message + } + c.ws.Close() +} + +func (c *connection) writer() { + for message := range c.send { + err := websocket.Message.Send(c.ws, message) + if err != nil { + break + } + } + c.ws.Close() +} + +func wsHandler(ws *websocket.Conn) { + c := &connection{send: make(chan string, 256), ws: ws} + h.register <- c + defer func() { h.unregister <- c }() + go c.writer() + c.reader() +} \ No newline at end of file Added: branches/ng-jtimer/jtimer-server/src/main/go/hub.go =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/hub.go (rev 0) +++ branches/ng-jtimer/jtimer-server/src/main/go/hub.go 2013-07-26 20:04:45 UTC (rev 2916) @@ -0,0 +1,44 @@ +package main + +type hub struct { + // Registered connections. + connections map[*connection]bool + + // Inbound messages from the connections. + broadcast chan string + + // Register requests from the connections. + register chan *connection + + // Unregister requests from connections. + unregister chan *connection +} + +var h = hub{ + broadcast: make(chan string), + register: make(chan *connection), + unregister: make(chan *connection), + connections: make(map[*connection]bool), +} + +func (h *hub) run() { + for { + select { + case c := <-h.register: + h.connections[c] = true + case c := <-h.unregister: + delete(h.connections, c) + close(c.send) + case m := <-h.broadcast: + for c := range h.connections { + select { + case c.send <- m: + default: + delete(h.connections, c) + close(c.send) + go c.ws.Close() + } + } + } + } +} Modified: branches/ng-jtimer/jtimer-server/src/main/go/jtimer.go =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/jtimer.go 2013-07-26 17:24:01 UTC (rev 2915) +++ branches/ng-jtimer/jtimer-server/src/main/go/jtimer.go 2013-07-26 20:04:45 UTC (rev 2916) @@ -1,35 +1,42 @@ +/* + * Copyright (C) 2013 CodeLutin, Chatellier Eric + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + */ + package main +/** + * Start a web socket server on port 12345. + * Notify idleness on ws://localhost:12345/jtimer + * + * jTimer ui can connect to ws://localhost:12345/jtimer to receive idle activity + */ + import ( "fmt" "net/http" "time" - //"log" - //"strconv" "github.com/BurntSushi/xgb" "github.com/BurntSushi/xgb/xproto" "github.com/BurntSushi/xgb/screensaver" "code.google.com/p/go.net/websocket" + "log" ) - -// echo back the websocket. -func EchoServer(ws *websocket.Conn) { - var message string - websocket.Message.Receive(ws, &message) - fmt.Println("[Received] ", message) -} - -func WebSocketServer() { - http.Handle("/echo", websocket.Handler(EchoServer)); - err := http.ListenAndServe(":12345", nil); - if err != nil { - //panic("ListenAndServe: ", err.String()) - fmt.Println("[Debug] "); - } -} - -func RunTimer() { +func RunIdleReporting() { X, err := xgb.NewConn() if err != nil { fmt.Println(err) @@ -42,8 +49,8 @@ screensaver.Init(X) // connect to webscocket server - origin := "http://localhost:12345/" - url := "ws://localhost:12345/echo" + origin := "http://localhost:8080/" + url := "ws://localhost:12345/jtimer" for { time.Sleep(1 * time.Second) @@ -53,14 +60,19 @@ ws, _ := websocket.Dial(url, "", origin) msSinceUserInputString := fmt.Sprintf("{idle:%d}", reply.MsSinceUserInput) - /*if _, err := ws.Write([]byte(msSinceUserInputString)); err != nil { - log.Fatal(err) - }*/ websocket.Message.Send(ws, msSinceUserInputString) } } +func StartWebsocketServer() { + go h.run() + http.Handle("/jtimer", websocket.Handler(wsHandler)) + if err := http.ListenAndServe(":12345", nil); err != nil { + log.Fatal("ListenAndServe:", err) + } +} + func main() { - go WebSocketServer() - RunTimer() + go StartWebsocketServer() + RunIdleReporting() } Added: branches/ng-jtimer/jtimer-server/src/main/go/readme.txt =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/readme.txt (rev 0) +++ branches/ng-jtimer/jtimer-server/src/main/go/readme.txt 2013-07-26 20:04:45 UTC (rev 2916) @@ -0,0 +1,16 @@ +Compilation do client go +======================== + +Packages necessaires +-------------------- +go get github.com/BurntSushi/xgb/screensaver +go get code.google.com/p/go.net/websocket + +Lancement +--------- +go run *.go + + +Compilation +----------- +go build -o jtimer *.go Modified: branches/ng-jtimer/jtimer-server/src/main/webapp/js/controllers.js =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/webapp/js/controllers.js 2013-07-26 17:24:01 UTC (rev 2915) +++ branches/ng-jtimer/jtimer-server/src/main/webapp/js/controllers.js 2013-07-26 20:04:45 UTC (rev 2916) @@ -321,4 +321,12 @@ // force the first tree creation $scope.createTree(); + + var conn = conn = new WebSocket("ws://localhost:12345/jtimer"); + conn.onclose = function(evt) { + console.log("Connection closed."); + }; + conn.onmessage = function(evt) { + console.log("Message", evt.data); + }; }
participants (1)
-
echatellier@users.chorem.org