-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgopheron.go
114 lines (98 loc) · 2.56 KB
/
gopheron.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
106
107
108
109
110
111
112
113
114
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
socketio "github.com/googollee/go-socket.io"
)
func socketIoServer(port int) {
server, err := socketio.NewServer(nil)
if err != nil {
log.Fatal(err)
}
server.On("connection", func(so socketio.Socket) {
log.Println("on connection")
so.Join("gopher")
so.On("electron start", func(msg string) {
log.Println("recv electron: " + msg)
})
so.On("gopher", func(msg string) {
//so.Emit("news","こんにちは");
})
so.On("gopher send", func(msg string) {
log.Println("gopher send :", msg)
so.BroadcastTo("gopher", "news", msg)
so.Emit("gopher recv", "send: ok")
})
so.On("gopher sendHtml", func(msg string) {
// log.Println("gopher sendHtml :", msg)
so.BroadcastTo("gopher", "writeHtml", msg)
so.Emit("gopher recv", "send: ok writeHtml")
})
so.On("gopher stop", func(msg string) {
log.Println("gopher stop :", msg)
so.BroadcastTo("gopher", "stop", msg)
so.Emit("gopher recv", "send: ok")
})
so.On("gopher front", func(msg string) {
log.Println("gopher front :", msg)
so.BroadcastTo("gopher", "front", msg)
so.Emit("gopher recv", "send: ok front")
})
so.On("disconnection", func() {
log.Println("on disconnect")
})
})
server.On("error", func(so socketio.Socket, err error) {
log.Println("error:", err)
})
http.Handle("/socket.io/", server)
http.Handle("/", http.FileServer(http.Dir("./asset")))
log.Println("Serving at localhost:" + strconv.Itoa(port) + "...")
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
}
func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func getElectronPath(gopheronPath string) string {
localElectron := gopheronPath + "/" + "node_modules/.bin/electron"
electronPath := "electron"
if exists(localElectron) {
return filepath.FromSlash(localElectron)
}
return electronPath
}
func getGopath() string {
goPath := os.Getenv("GOPATH")
if len(goPath) == 0 {
if runtime.GOOS == "windows" {
goPath = os.Getenv("USERPROFILE") + "/go"
} else {
goPath = os.Getenv("HOME") + "/go"
}
}
return goPath
}
func startElectron() {
gopheronPath := path.Dir(os.Args[0])
gopheronPath = getGopath() + "/src/github.com/kjunichi/gopheron"
os.Chdir(gopheronPath)
out, err := exec.Command(getElectronPath(gopheronPath), "--with-golang", gopheronPath).Output()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(string(out))
}
func main() {
go socketIoServer(5050)
startElectron()
//time.Sleep(10000 * time.Second)
}