receiving messages from player now

master
Jordan Orelli 5 years ago
parent 00ba6cf16e
commit b40a0f9ec4

1
server/.gitignore vendored

@ -0,0 +1 @@
kloam

@ -3,6 +3,8 @@ module github.com/jordanorelli/kloam
go 1.13 go 1.13
require ( require (
github.com/gorilla/mux v1.7.3
github.com/gorilla/websocket v1.4.1
github.com/jordanorelli/blammo v0.0.0-20200201231725-9fd70eb7c1f2 github.com/jordanorelli/blammo v0.0.0-20200201231725-9fd70eb7c1f2
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect
) )

@ -1,3 +1,7 @@
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/jordanorelli/blammo v0.0.0-20200121053404-bd13f6b43c9f h1:m1UX7IV22aEt+hw5R/9MkmB2jdF4kRucP9Z/E/ql1JU= github.com/jordanorelli/blammo v0.0.0-20200121053404-bd13f6b43c9f h1:m1UX7IV22aEt+hw5R/9MkmB2jdF4kRucP9Z/E/ql1JU=
github.com/jordanorelli/blammo v0.0.0-20200121053404-bd13f6b43c9f/go.mod h1:ofUhNTXhWoL77wTBOnWpjPN9s244RK1Hn0XiYhE4E1s= github.com/jordanorelli/blammo v0.0.0-20200121053404-bd13f6b43c9f/go.mod h1:ofUhNTXhWoL77wTBOnWpjPN9s244RK1Hn0XiYhE4E1s=
github.com/jordanorelli/blammo v0.0.0-20200201231725-9fd70eb7c1f2 h1:DH2VZ5Z4paQJe0zViSQENrwbgfrt4yyTeckjureCKOw= github.com/jordanorelli/blammo v0.0.0-20200201231725-9fd70eb7c1f2 h1:DH2VZ5Z4paQJe0zViSQENrwbgfrt4yyTeckjureCKOw=

@ -1,6 +1,8 @@
package main package main
import ( import (
"net"
"net/http"
"os" "os"
"github.com/jordanorelli/blammo" "github.com/jordanorelli/blammo"
@ -12,4 +14,15 @@ func main() {
log := blammo.NewLog("kloam", blammo.DebugWriter(stdout), blammo.InfoWriter(stdout), blammo.ErrorWriter(stderr)) log := blammo.NewLog("kloam", blammo.DebugWriter(stdout), blammo.InfoWriter(stdout), blammo.ErrorWriter(stderr))
log.Info("this is some info") log.Info("this is some info")
log.Info("some more info here") log.Info("some more info here")
s := server{
Log: log,
}
s.init()
lis, err := net.Listen("tcp", "0.0.0.0:9001")
if err != nil {
log.Error("listen error: %v", err)
return
}
http.Serve(lis, s.handler())
} }

@ -0,0 +1,10 @@
package main
import "github.com/gorilla/websocket"
type player struct {
conn *websocket.Conn
}
func (p *player) readMessages() {
}

@ -0,0 +1,55 @@
package main
import (
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/jordanorelli/blammo"
)
type server struct {
*blammo.Log
router *mux.Router
}
func (s *server) init() {
r := mux.NewRouter()
r.HandleFunc("/kloam", s.play)
s.router = r
}
func (s *server) handler() http.Handler {
return s.router
}
func (s *server) play(w http.ResponseWriter, r *http.Request) {
u := websocket.Upgrader{}
conn, err := u.Upgrade(w, r, nil)
if err != nil {
s.Error("upgrade error: %s", err)
return
}
defer conn.Close()
s.Info("client connected: %v", conn.RemoteAddr())
for {
_, rd, err := conn.NextReader()
if err != nil {
s.Error("nextreader error: %s", err)
break
}
b, err := ioutil.ReadAll(rd)
if err != nil {
s.Error("read error: %s", err)
} else {
s.Child("rcv").Info(string(b))
}
}
}
Loading…
Cancel
Save