Permalink
Browse files

Server scaffolding

  • Loading branch information...
1 parent ece49c8 commit 552cc0f4323b46f4340f51cb01e856ec6e57334b @brendonh committed Feb 22, 2013
View
@@ -0,0 +1,3 @@
+bin
+data
+pkg
View
@@ -0,0 +1,3 @@
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+export GOPATH=$DIR
+export PATH=$PATH:$GOPATH/bin
View
@@ -0,0 +1 @@
+github.com
@@ -0,0 +1,16 @@
+package gambit
+
+import (
+ "fmt"
+
+ . "github.com/brendonh/go-service"
+
+ "code.google.com/p/go.net/websocket"
+)
+
+
+func GameMessageHandler(endpoint *WebsocketEndpoint, buf []byte,
+ session Session, conn *websocket.Conn) {
+
+ fmt.Printf("Got: %v\n", buf)
+}
@@ -0,0 +1,26 @@
+package gambit
+
+import (
+ "github.com/brendonh/go-service"
+ "github.com/brendonh/loge/src/loge"
+)
+
+type GambitServer struct {
+ goservice.Server
+ DB *loge.LogeDB
+}
+
+func BuildServer(dbpath string) *GambitServer {
+ var services = goservice.NewServiceCollection()
+
+ var server = goservice.NewServer(services, goservice.BasicSessionCreator)
+
+ var websocketEndpoint = goservice.NewWebsocketEndpoint(":4567", server)
+ websocketEndpoint.Handler = GameMessageHandler
+ server.AddEndpoint(websocketEndpoint)
+
+ return &GambitServer {
+ *server,
+ loge.NewLogeDB(loge.NewLevelDBStore(dbpath)),
+ }
+}
View
@@ -0,0 +1,27 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "os/signal"
+
+ "server/gambit"
+)
+
+
+func main() {
+ var server = gambit.BuildServer("data/gambit")
+ fmt.Printf("Server: %v\n", server)
+
+ var stopper = make(chan os.Signal, 1)
+ signal.Notify(stopper)
+
+ server.Start()
+
+ <-stopper
+ close(stopper)
+
+ fmt.Printf("\nShutting down ...\n")
+ server.Stop()
+ server.DB.Close()
+}
View
@@ -9,6 +9,12 @@ define([
_.extend(Game.prototype, {
start: function() {
console.log("Starting game", this.id);
+
+ if (!this.initSocket()) {
+ alert("No websockets!");
+ return;
+ }
+
$("#main").html(_.template(tmplGame)({ gameID: this.id }));
this.canvas = $("#game-" + this.id).get(0);
@@ -20,6 +26,25 @@ define([
this.render();
},
+ initSocket: function() {
+ var socket;
+ if ("WebSocket" in window) {
+ socket = window.WebSocket;
+ } else if ("MozWebSocket" in window) {
+ socket = window.MozWebSocket;
+ }
+
+ if (!socket) {
+ return false;
+ }
+
+ this.socket = new socket("ws://gambit.brendonh.org:4567");
+ this.socket.onopen = _.bind(this.onConnect, this);
+
+ return true;
+ },
+
+
initContext: function() {
try {
var gl = this.canvas.getContext("webgl")
@@ -39,6 +64,11 @@ define([
return true;
},
+ onConnect: function() {
+ console.log("Connected!");
+ this.socket.send("Hello");
+ },
+
render: function() {
var gl = this.gl;
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);

0 comments on commit 552cc0f

Please sign in to comment.