From b40a0f9ec408d1923d6ea6121d1bbd9243235d10 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 2 Feb 2020 03:26:47 +0000 Subject: [PATCH] receiving messages from player now --- server/.gitignore | 1 + server/go.mod | 2 ++ server/go.sum | 4 ++++ server/main.go | 13 +++++++++++ server/player.go | 10 +++++++++ server/server.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+) create mode 100644 server/.gitignore create mode 100644 server/player.go create mode 100644 server/server.go diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..2ddc6ca --- /dev/null +++ b/server/.gitignore @@ -0,0 +1 @@ +kloam diff --git a/server/go.mod b/server/go.mod index 3c5d440..b5d0d74 100644 --- a/server/go.mod +++ b/server/go.mod @@ -3,6 +3,8 @@ module github.com/jordanorelli/kloam go 1.13 require ( + github.com/gorilla/mux v1.7.3 + github.com/gorilla/websocket v1.4.1 github.com/jordanorelli/blammo v0.0.0-20200201231725-9fd70eb7c1f2 golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect ) diff --git a/server/go.sum b/server/go.sum index 6869562..a001543 100644 --- a/server/go.sum +++ b/server/go.sum @@ -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/go.mod h1:ofUhNTXhWoL77wTBOnWpjPN9s244RK1Hn0XiYhE4E1s= github.com/jordanorelli/blammo v0.0.0-20200201231725-9fd70eb7c1f2 h1:DH2VZ5Z4paQJe0zViSQENrwbgfrt4yyTeckjureCKOw= diff --git a/server/main.go b/server/main.go index ba64743..cf29676 100644 --- a/server/main.go +++ b/server/main.go @@ -1,6 +1,8 @@ package main import ( + "net" + "net/http" "os" "github.com/jordanorelli/blammo" @@ -12,4 +14,15 @@ func main() { log := blammo.NewLog("kloam", blammo.DebugWriter(stdout), blammo.InfoWriter(stdout), blammo.ErrorWriter(stderr)) log.Info("this is some info") 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()) } diff --git a/server/player.go b/server/player.go new file mode 100644 index 0000000..ee62aae --- /dev/null +++ b/server/player.go @@ -0,0 +1,10 @@ +package main + +import "github.com/gorilla/websocket" + +type player struct { + conn *websocket.Conn +} + +func (p *player) readMessages() { +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..5e1fafc --- /dev/null +++ b/server/server.go @@ -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)) + } + } + +}