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)
}

4
add.go
View File

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

View File

@ -9,7 +9,7 @@ type Loggedin struct {
Loggedin bool
}
func adminHandler (c echo.Context) error {
func adminHandler(c echo.Context) error {
rw := c.Response()
r := c.Request()

View File

@ -6,4 +6,24 @@ setInterval(function() {
$( "#konfis" ).append('<tr> <td>' + item.Name + '</td> <td>' + item.Gemeinde + '</td> <td>' + item.KCoins + '</td></tr>');
});
});
}, 1000);
}, 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);
};*/

View File

@ -8,8 +8,8 @@ import (
//Configuration Struct
type Configuration struct {
AdminPassword string
Interface string
DBFile string
Interface string
DBFile string
}
var SiteConf Configuration = Configuration{}

12
db.go
View File

@ -1,15 +1,15 @@
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
func DBinit() *xorm.Engine{
func DBinit() *xorm.Engine {
SiteConf := initConfig()
db, err := xorm.NewEngine("sqlite3", SiteConf.DBFile)
if err != nil {
@ -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

@ -18,7 +18,7 @@ func deleteKonfi(c echo.Context) error {
logged := sess.Get("login")
//Wenn das password stimmt
if logged != nil{
if logged != nil {
id, _ := strconv.Atoi(c.FormValue("id"))
//Löschen

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

@ -17,12 +17,11 @@ func login(c echo.Context) error {
var pass string = c.FormValue("password")
//Wenn das password stimmt, einloggen
if SiteConf.AdminPassword == pass{
if SiteConf.AdminPassword == pass {
sess.Set("login", true)
return c.JSON(http.StatusOK, Message{"success"})
} else {
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)

View File

@ -5,7 +5,7 @@ import (
"net/http"
)
func showList (c echo.Context) error {
func showList(c echo.Context) error {
//Template
return c.Render(http.StatusOK, "index", Message{"schinken"})
}

View File

@ -18,7 +18,7 @@ func update(c echo.Context) error {
logged := sess.Get("login")
//Wenn das password stimmt
if logged != nil{
if logged != nil {
id, _ := strconv.Atoi(c.FormValue("id"))
addcoins, _ := strconv.Atoi(c.FormValue("addcoins"))

View File

@ -3,10 +3,10 @@ package main
import "log"
type Kofi struct {
ID int `xorm:"pk autoincr"`
Name string
ID int `xorm:"pk autoincr"`
Name string
Gemeinde string
KCoins int
KCoins int
}
type Message struct {
@ -15,7 +15,7 @@ type Message struct {
type UpdatedMessage struct {
Message string
Kofi Kofi
Kofi Kofi
}
//CheckError
@ -23,4 +23,4 @@ func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
}

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
}