diff --git a/config.ini b/.env similarity index 56% rename from config.ini rename to .env index 59fb5a6..a194787 100644 --- a/config.ini +++ b/.env @@ -4,8 +4,9 @@ SOCKET_PORT=8900 # SOCKET_CERT_FILE= # SOCKET_KEY_FILE= -GATEWAY_API_ADDRESS=127.0.0.1:8901 +#GATEWAY_API_ADDRESS=192.168.3.92:8901 +GATEWAY_API_ADDRESS=192.168.3.92:8901 GATEWAY_API_TOKEN=token -NOTIFICATION_URL=http://message.demo.com/im/index/rpc +NOTIFICATION_URL=http://sdk.demo.com NOTIFICATION_USER_AGENT="Gopusher 1.0" diff --git a/comet/comet.go b/comet/comet.go index 0022d34..a4f4463 100644 --- a/comet/comet.go +++ b/comet/comet.go @@ -1,7 +1,6 @@ package comet import ( - "github.com/gopusher/gateway/notification" "github.com/gopusher/gateway/configuration" "github.com/gopusher/gateway/contracts" "github.com/gopusher/gateway/connection/websocket" @@ -15,17 +14,17 @@ func Run() { go server.Run() + go server.JoinCluster() + api.InitRpcServer(server, config) } func getCometServer(config *configuration.CometConfig) contracts.Server { - rpc := notification.NewRpc(config.NotificationUrl, config.NotificationUserAgent) - switch config.SocketProtocol { case "ws": fallthrough case "wss": - return websocket.NewWebSocketServer(config, rpc) + return websocket.NewWebSocketServer(config) case "tcp": //暂时不处理 panic("Unsupported protocol: " + config.SocketProtocol) default: diff --git a/connection/websocket/server.go b/connection/websocket/server.go index 33a22dd..a612f8b 100644 --- a/connection/websocket/server.go +++ b/connection/websocket/server.go @@ -7,6 +7,7 @@ import ( "errors" "github.com/gopusher/gateway/notification" "github.com/gopusher/gateway/log" + "time" ) type Server struct { @@ -18,7 +19,7 @@ type Server struct { clients map[string]*Client } -func NewWebSocketServer(config *configuration.CometConfig, rpc *notification.Client) *Server { +func NewWebSocketServer(config *configuration.CometConfig) *Server { var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, @@ -27,6 +28,7 @@ func NewWebSocketServer(config *configuration.CometConfig, rpc *notification.Cli }, } + rpc := notification.NewRpc(config.NotificationUrl, config.NotificationUserAgent) return &Server{ config: config, rpc: rpc, @@ -210,3 +212,14 @@ func (s *Server) GetAllConnections() []string { return connectionIds } + +func (s *Server) JoinCluster() { + //wait for rpc and ws server bootstrap + time.Sleep(time.Duration(5)*time.Second) + //notify router api server + if _, err := s.rpc.Call("JoinCluster", s.config.NodeId); err != nil { + log.Error("Gateway JoinCluster notification failed: %s", err.Error()) + } + + log.Info("Gateway JoinCluster notification success") +} diff --git a/contracts/connection.go b/contracts/connection.go index 1634de4..56153de 100644 --- a/contracts/connection.go +++ b/contracts/connection.go @@ -8,4 +8,5 @@ type Server interface { KickAllConnections() CheckConnectionsOnline(connections []string) []string GetAllConnections() []string + JoinCluster() }