Files
open-im-server/internal/msggateway/init.go
T

78 lines
2.4 KiB
Go
Raw Normal View History

2023-07-04 11:15:20 +08:00
// Copyright © 2023 OpenIM. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package msggateway
import (
2024-04-19 22:23:08 +08:00
"context"
"time"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
2024-07-16 10:46:21 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/rpccache"
"github.com/openimsdk/tools/db/redisutil"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/utils/datautil"
"github.com/openimsdk/tools/utils/runtimeenv"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/log"
)
2024-04-19 22:23:08 +08:00
type Config struct {
2024-05-14 18:21:36 +08:00
MsgGateway config.MsgGateway
Share config.Share
2024-07-16 10:46:21 +08:00
RedisConfig config.Redis
2024-05-14 18:21:36 +08:00
WebhooksConfig config.Webhooks
Discovery config.Discovery
RuntimeEnv string
2024-04-19 22:23:08 +08:00
}
// Start run ws server.
func Start(ctx context.Context, index int, conf *Config) error {
conf.RuntimeEnv = runtimeenv.PrintRuntimeEnvironment()
2024-12-10 11:43:19 +08:00
log.CInfo(ctx, "MSG-GATEWAY server is initializing", "runtimeEnv", conf.RuntimeEnv,
2024-12-06 17:25:42 +08:00
"rpcPorts", conf.MsgGateway.RPC.Ports,
2024-04-19 22:23:08 +08:00
"wsPort", conf.MsgGateway.LongConnSvr.Ports, "prometheusPorts", conf.MsgGateway.Prometheus.Ports)
wsPort, err := datautil.GetElemByIndex(conf.MsgGateway.LongConnSvr.Ports, index)
if err != nil {
return err
}
2024-07-16 10:46:21 +08:00
rdb, err := redisutil.NewRedisClient(ctx, conf.RedisConfig.Build())
if err != nil {
return err
}
longServer := NewWsServer(
conf,
WithPort(wsPort),
2024-04-19 22:23:08 +08:00
WithMaxConnNum(int64(conf.MsgGateway.LongConnSvr.WebsocketMaxConnNum)),
WithHandshakeTimeout(time.Duration(conf.MsgGateway.LongConnSvr.WebsocketTimeout)*time.Second),
WithMessageMaxMsgLength(conf.MsgGateway.LongConnSvr.WebsocketMaxMsgLen),
2023-11-22 17:31:37 +08:00
)
2023-11-29 10:44:37 +08:00
hubServer := NewServer(longServer, conf, func(srv *Server) error {
2024-09-12 10:38:17 +08:00
longServer.online, _ = rpccache.NewOnlineCache(srv.userRcp, nil, rdb, false, longServer.subscriberUserOnlineStatusChanges)
2024-07-16 10:46:21 +08:00
return nil
})
go longServer.ChangeOnlineStatus(4)
netDone := make(chan error)
go func() {
2024-04-19 22:23:08 +08:00
err = hubServer.Start(ctx, index, conf)
netDone <- err
}()
return hubServer.LongConnServer.Run(netDone)
}