/
main.go
105 lines (88 loc) · 2.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"flag"
"fmt"
"github.com/googollee/go-socket.io"
"github.com/stefanbeeman/antfarm"
"log"
"net/http"
"os"
"time"
)
var (
hostname string
port int
topStaticDir string
game antfarm.Game
)
func init() {
// Flags
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [default_static_dir]\n", os.Args[0])
flag.PrintDefaults()
}
flag.StringVar(&hostname, "h", "localhost", "hostname")
flag.IntVar(&port, "p", 8080, "port")
flag.StringVar(&topStaticDir, "static_dir", "", "static directory in addition to default static directory")
game = antfarm.MakeGame("../antfarm-data", 20, 20, 1)
}
func appendStaticRoute(sr StaticRoutes, dir string) StaticRoutes {
if _, err := os.Stat(dir); err != nil {
log.Fatal(err)
}
return append(sr, http.Dir(dir))
}
type StaticRoutes []http.FileSystem
func (sr StaticRoutes) Open(name string) (f http.File, err error) {
for _, s := range sr {
if f, err = s.Open(name); err == nil {
f = disabledDirListing{f}
return
}
}
return
}
type disabledDirListing struct {
http.File
}
func (f disabledDirListing) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
}
func onConnect(ns *socketio.NameSpace) {
fmt.Println("connected:", ns.Id())
ns.Emit("game", game.Display())
}
func onDisconnect(ns *socketio.NameSpace) {
fmt.Println("disconnected:", ns.Id())
}
func main() {
// Parse flags
flag.Parse()
staticDir := flag.Arg(0)
// Setup static routes
staticRoutes := make(StaticRoutes, 0)
if topStaticDir != "" {
staticRoutes = appendStaticRoute(staticRoutes, topStaticDir)
}
if staticDir == "" {
staticDir = "./"
}
staticRoutes = appendStaticRoute(staticRoutes, staticDir)
sock_config := &socketio.Config{}
sock_config.HeartbeatTimeout = 2
sock_config.ClosingTimeout = 4
sio := socketio.NewSocketIOServer(sock_config)
sio.On("connect", onConnect)
sio.On("disconnect", onDisconnect)
ticker := time.NewTicker(time.Millisecond * 100)
go func() {
for t := range ticker.C {
game.RunFor(1)
sio.Broadcast("game", game.Display(), t)
}
}()
//this will serve a http static file server
sio.Handle("/", http.FileServer(staticRoutes))
//startup the server
http.ListenAndServe(":9000", sio)
}