forked from pschlump/socketio
/
main.go
76 lines (63 loc) · 2.21 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
package main
//
// Command line arguments can be used to set the IP address that is listened to and the port.
//
// $ ./chat --port=8080 --host=127.0.0.1 --dir=./www
//
// Bring up a pair of browsers and chat between them.
//
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/pschlump/MiscLib"
"github.com/pschlump/godebug"
"github.com/mlsquires/socketio"
)
var Port = flag.String("port", "9000", "Port to listen to") // 0
var HostIP = flag.String("host", "localhost", "Host name or IP address to listen on") // 1
var Dir = flag.String("dir", "./asset", "Direcotry where files are served from") // 2
func init() {
flag.StringVar(Port, "P", "9000", "Port to listen to") // 0
flag.StringVar(HostIP, "H", "localhost", "Host name or IP address to listen on") // 1
flag.StringVar(Dir, "d", "./asset", "Direcotry where files are served from") // 2
}
func main() {
flag.Parse()
fns := flag.Args()
if len(fns) != 0 {
fmt.Printf("Usage: Invalid arguments supplied, %s\n", fns)
os.Exit(1)
}
var host_ip string = ""
if *HostIP != "localhost" {
host_ip = *HostIP
}
// Make certain that the command line parameters are handled correctly
// fmt.Printf("host_ip >%s< HostIP >%s< Port >%s<\n", host_ip, *HostIP, *Port)
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.On("connection", func(so socketio.Socket) {
fmt.Printf("%sa user connected%s, %s\n", MiscLib.ColorGreen, MiscLib.ColorReset, godebug.LF())
so.Join("chat")
so.On("chat message", func(msg string) {
fmt.Printf("%schat message, %s%s, %s\n", MiscLib.ColorGreen, msg, MiscLib.ColorReset, godebug.LF())
so.BroadcastTo("chat", "chat message", msg)
})
so.On("disconnect", func() {
fmt.Printf("%suser disconnect%s, %s\n", MiscLib.ColorYellow, MiscLib.ColorReset, godebug.LF())
})
})
server.On("error", func(so socketio.Socket, err error) {
fmt.Printf("Error: %s, %s\n", err, godebug.LF())
})
http.Handle("/socket.io/", server)
http.Handle("/", http.FileServer(http.Dir(*Dir)))
fmt.Printf("Serving on port %s, browse to http://localhost:%s/\n", *Port, *Port)
listen := fmt.Sprintf("%s:%s", host_ip, *Port)
log.Fatal(http.ListenAndServe(listen, nil))
}