forked from woodylan/go-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (61 loc) · 1.44 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
import (
"fmt"
"go-websocket/define"
"go-websocket/pkg/redis"
"go-websocket/routers"
"go-websocket/servers"
_ "go-websocket/tools/log"
"go-websocket/tools/readconfig"
"go-websocket/tools/util"
"net/http"
"os"
)
func main() {
port := getPort()
//初始化配置文件
if err := readconfig.InitConfig(); err != nil {
panic(err)
}
//初始化RPC服务
initRPCServer(port)
//记录本机内网IP地址
define.LocalHost = util.GetIntranetIp()
//将服务器地址、端口注册到redis中
registerServer()
//初始化路由
routers.Init()
//启动一个定时器用来发送心跳
servers.PingTimer()
fmt.Printf("服务器启动成功,端口号:%s\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
panic(err)
}
}
func initRPCServer(port string) {
//如果是集群,则启用RPC进行通讯
if util.IsCluster() {
//初始化RPC服务
rpcPort := util.GenRpcPort(port)
servers.InitRpcServer(rpcPort)
fmt.Printf("启动RPC,端口号:%s\n", rpcPort)
}
}
//将服务器地址、端口注册到redis中
func registerServer() {
if util.IsCluster() {
_, err := redis.SetAdd(define.REDIS_KEY_SERVER_LIST, define.LocalHost+":"+define.RPCPort)
if err != nil {
panic(err)
}
}
}
func getPort() string {
port := "666"
args := os.Args //获取用户输入的所有参数
if args != nil && len(args) >= 2 && len(args[1]) != 0 {
port = args[1]
}
define.Port = port
return port
}