Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
"space-infix-ops": "error",
"no-unused-vars": "off"
}
}
}
2 changes: 1 addition & 1 deletion client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ const config = {

const game = new Phaser.Game(config);

window.addEventListener('resize', () => game.scale.resize(window.innerWidth, window.innerHeight));
window.addEventListener('resize', () => game.scale.resize(window.innerWidth, window.innerHeight));
3 changes: 1 addition & 2 deletions client/src/objects/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ export class Player extends Phaser.Physics.Arcade.Sprite {
this.setTexture('stand');
}
}
}

}
33 changes: 27 additions & 6 deletions server/cmd/ptrun-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ package main
import (
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/spf13/cobra"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/spf13/cobra"
"go.uber.org/zap"
"goji.io"
"goji.io/pat"

"github.com/ptr-geeks/ptrun/server/internal/ws"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
Expand All @@ -28,6 +32,13 @@ type ServerConfig struct {
Path string
}

var (
state = promauto.NewGauge(prometheus.GaugeOpts{
Name: "ptrun_state",
Help: "State",
})
)

func main() {
config := ServerConfig{}

Expand Down Expand Up @@ -66,19 +77,27 @@ func run(config *ServerConfig) {

sugared := logger.Sugar()

state.Set(0)

mux := goji.NewMux()
mux.HandleFunc(pat.Get("/ws"), func(w http.ResponseWriter, r *http.Request) {
server.Connect(w, r)
})
mux.HandleFunc(pat.Get("/metrics"), func(w http.ResponseWriter, r *http.Request) {
prometheus := promhttp.Handler()
prometheus.ServeHTTP(w, r)
})

server = ws.NewServer(logger)
go server.Run()

// TODO: We should allow some overrides by passing parameters to our executable
// Example: ./ptr-server --port 8080 --path "/ws"
sugared.Infow("starting websocket endpoint",
"host", config.Host,
"port", config.Port,
"path", config.Path)

mux := goji.NewMux()
mux.HandleFunc(pat.Get(config.Path), func(w http.ResponseWriter, r *http.Request) {
server.Connect(w, r)
})

srv := &http.Server{
Handler: mux,
Addr: fmt.Sprintf("%s:%s", config.Host, config.Port),
Expand All @@ -90,9 +109,11 @@ func run(config *ServerConfig) {

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
state.Set(1)

go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
state.Set(2)
sugared.Errorw("error starting http server", zap.Error(err))
}
}()
Expand Down
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
github.com/gorilla/websocket v1.4.2
github.com/prometheus/client_golang v1.11.0
github.com/spf13/cobra v1.2.1
go.uber.org/zap v1.19.0
goji.io v2.0.2+incompatible
Expand Down
80 changes: 77 additions & 3 deletions server/go.sum

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions server/internal/ws/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ws

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"net/http"

"github.com/gorilla/websocket"
Expand All @@ -23,6 +25,13 @@ var (
}
)

var (
clientscounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "ptrun_client_counter",
Help: "Currently connected clients in WebSockets",
})
)

// serevrImpl has data about all clients
// and sends the connect and disconnect messages
type serverImpl struct {
Expand Down Expand Up @@ -65,6 +74,7 @@ func (s *serverImpl) Run() {
for {
select {
case connect := <-s.connect:
clientscounter.Inc()
s.logger.Infow("new connection", "remoteAddr", connect.conn.RemoteAddr())
client := NewClient(connect.conn, s, s.logger.Desugar())
s.clients[client.GetID()] = client
Expand Down
1 change: 1 addition & 0 deletions server/internal/ws/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Server interface {

Connect(w http.ResponseWriter, r *http.Request) Client
Disconnect(client Client)

Broadcast(excludeClient int32, msg *messages.Message)
}

Expand Down