websocket hinzugefügt

This commit is contained in:
konrad 2017-08-30 22:23:35 +02:00 committed by kolaente
parent fae7eec5ea
commit fc139036c0
15 changed files with 87 additions and 27 deletions

View File

@ -9,6 +9,6 @@ TODO::
* ~~Manuell (einzeln)~~
* CSV-Import
* ~~Kofis löschen~~
* Konfis auf der Frontseite mit Websockets updaten
* ~~Logout~~
* Konfis auf der Frontseite mit Websockets updaten
* Mode hinzufügen: in der Config soll man zwischen Gemeinden und Konfis umschalten können, so dass entweder die Gemeinden oder Konfis gegeneinander spielen

View File

@ -13,4 +13,3 @@ type Template struct {
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}

2
add.go
View File

@ -16,7 +16,7 @@ func addKonfi(c echo.Context) error {
sess := GlobalSessions.SessionStart(rw, r)
logged := sess.Get("login")
//Wenn das password stimmt
//Wenn eingeloggt
if logged != nil {
kofi := new(Kofi)
kofi.Name = c.FormValue("name")

View File

@ -7,3 +7,23 @@ setInterval(function() {
});
});
}, 1000);
/*
var loc = window.location;
var uri = 'ws:';
if (loc.protocol === 'https:') {
uri = 'wss:';
}
uri += '//' + loc.host;
uri += loc.pathname + 'ws';
ws = new WebSocket(uri)
ws.onopen = function() {
console.log('Connected');
ws.send('get');
};
ws.onmessage = function(evt) {
console.log(evt.data);
};*/

8
db.go
View File

@ -1,10 +1,10 @@
package main
import (
_ "github.com/mattn/go-sqlite3"
"github.com/go-xorm/xorm"
"github.com/go-xorm/core"
"fmt"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
)
var db *xorm.Engine
@ -17,7 +17,7 @@ func DBinit() *xorm.Engine{
}
db.SetMapper(core.SameMapper{})
db.ShowSQL(true)
db.ShowSQL(false)
db.Logger().SetLevel(core.LOG_DEBUG)
return db
}

View File

@ -1,9 +1,9 @@
package main
import (
"fmt"
"github.com/labstack/echo"
"net/http"
"fmt"
)
func getList(c echo.Context) error {

View File

@ -24,5 +24,4 @@ func login(c echo.Context) error {
return c.JSON(http.StatusOK, Message{"fail"})
}
}

View File

@ -3,12 +3,12 @@ package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"html/template"
"github.com/labstack/gommon/log"
"html/template"
"fmt"
"github.com/astaxie/session"
_ "github.com/astaxie/session/providers/memory"
"fmt"
)
//Initialize Session
@ -64,6 +64,7 @@ func main() {
e.GET("/list", getList)
e.GET("/admin", adminHandler)
e.GET("/logout", logout)
e.GET("/ws", ws)
e.POST("/login", login)
e.POST("/update", update)

41
ws.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"github.com/labstack/echo"
"golang.org/x/net/websocket"
"fmt"
"encoding/json"
)
func ws(c echo.Context) error {
//Datenbankverbindung aufbauen
db := DBinit()
// Websocket
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
for {
msg := ""
err := websocket.Message.Receive(ws, &msg)
if err != nil {
c.Logger().Error(err)
}
fmt.Println(msg)
//Daten holen und anzeigen
var kofi []Kofi
err = db.OrderBy("KCoins DESC").Find(&kofi)
if err != nil {
fmt.Println(err)
}
kofiJson, err := json.Marshal(kofi)
// Wegschicken
err = websocket.Message.Send(ws, string(kofiJson))
if err != nil {
c.Logger().Error(err)
}
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}