Skip to content

Commit

Permalink
Merge pull request #3 from xuexihuang/main
Browse files Browse the repository at this point in the history
 Implement  basic functions
  • Loading branch information
FGadvancer authored Jan 2, 2024
2 parents 9693813 + d9e49f7 commit 939678a
Show file tree
Hide file tree
Showing 26 changed files with 1,504 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# build container
FROM golang:1.20-alpine3.18 AS builder
ENV GOPROXY https://goproxy.cn,direct
ENV GOSUMDB=sum.golang.google.cn
ENV GO111MODULE=on

WORKDIR /app
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk --no-cache add git pkgconfig build-base
ADD go.mod .
ADD go.sum .
RUN go mod download
ADD . .
RUN go build -o cmd/openim-msggateway-proxy cmd/main.go

# archive container
FROM alpine:3.18
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk --no-cache add ca-certificates libdrm
RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata

WORKDIR /app/bin
COPY --from=builder /app/cmd/openim-msggateway-proxy /app/bin/
ENTRYPOINT ["/app/bin/openim-msggateway-proxy"]
11 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

IMAGEHUB="registry.cn-shenzhen.aliyuncs.com/huanglin_hub"
PROJECT="openim-msggateway-proxy"
ALLPRO="all"

echo "building ${PROJECT}"
DOCKER_PUSHIMG=${IMAGEHUB}/${PROJECT}:dev
docker rmi ${DOCKER_PUSHIMG}
docker build -f ./Dockerfile -t ${DOCKER_PUSHIMG} .
docker push ${DOCKER_PUSHIMG}
Binary file added cmd/cmd
Binary file not shown.
37 changes: 37 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
"github.com/bwmarrin/snowflake"
"github.com/openimsdk/openim-msggateway-proxy/common"
"github.com/openimsdk/openim-msggateway-proxy/gate"
"github.com/openimsdk/openim-msggateway-proxy/module"
log "github.com/xuexihuang/new_log15"
"os"
"os/signal"
"syscall"
)

func main() {
log.SetOutLevel(log.LvlInfo)
fmt.Println("客户端启动....")
log.Info("客户端启动....")
snode, err := snowflake.NewNode(200)
if err != nil {
log.Error("snowflake.NewNode error", "err", err)
return
}
common.G_flakeNode = snode
common.GatewayConsistent = common.NewGatewayConsistent()
///////////////////////////////////
gatenet := gate.InitWSsever(80)
gatenet.SetMsgFun(module.NewAgent, module.CloseAgent, module.DataRecv)
go gatenet.Runloop()
///////////////////////////////////////////
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGQUIT, syscall.SIGTERM)
sig := <-c
log.Info("wsconn server closing down ", "sig", sig)
gatenet.CloseGate()

}
44 changes: 44 additions & 0 deletions cmd/wsclienttest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"github.com/openimsdk/openim-msggateway-proxy/common"
"github.com/openimsdk/openim-msggateway-proxy/network"
log "github.com/xuexihuang/new_log15"
"time"
)

func main() {
client, err := network.NewWSClient("12345678", "127.0.0.1:80", "/ws?sendID=huanglin")
if err != nil {
log.Info("NewWSClient error")
return
}
defer client.Destroy()
body := `{
"type":3,
"tts_text":"你好"
}`
go func() {
for {
time.Sleep(2 * time.Second)
client.WriteMsg(&common.TWSData{MsgType: common.MessageText, Msg: []byte(body)})
time.Sleep(3 * time.Second)
client.WriteMsg(&common.TWSData{MsgType: common.PingMessage, Msg: []byte("pingmsg")})
time.Sleep(3 * time.Second)
client.WriteMsg(&common.TWSData{MsgType: common.PongMessage, Msg: []byte("pongmsg")})
}

}()

for {
select {
case clientMsg, _ := <-client.RecvMsgChan:
if clientMsg.MsgType == common.CloseMessage {
log.Info("ws net error")
return
} else {
log.Info("get one recv data", "msgType", clientMsg.MsgType)
}
}
}
}
Binary file added cmd/wsclienttest/wsclienttest
Binary file not shown.
40 changes: 40 additions & 0 deletions common/consistentHash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package common

import (
"context"
"fmt"
"github.com/stathat/consistent"
log "github.com/xuexihuang/new_log15"
"os"
"strconv"
"strings"
)

var GatewayConsistent *consistent.Consistent

func NewGatewayConsistent() *consistent.Consistent {
gatewayConsistent := consistent.New()
gatewayHosts := getMsgGatewayHost(context.Background())
for _, v := range gatewayHosts {
gatewayConsistent.Add(v)
}
return gatewayConsistent
}

// like openimserver-openim-msggateway-0.openimserver-openim-msggateway-headless.openim-lin.svc.cluster.local:80
func getMsgGatewayHost(ctx context.Context) []string {
instance := "openimserver"
selfPodName := os.Getenv("MY_POD_NAME")
replicas := os.Getenv("MY_MSGGATEWAY_REPLICACOUNT")
ns := os.Getenv("MY_POD_NAMESPACE")
nReplicas, _ := strconv.Atoi(replicas)
podInfo := strings.Split(selfPodName, "-")
instance = podInfo[0]
var ret []string
for i := 0; i < nReplicas; i++ {
host := fmt.Sprintf("%s-openim-msggateway-%d.%s-openim-msggateway-headless.%s.svc.cluster.local", instance, i, instance, ns)
ret = append(ret, host)
}
log.Info("msggateway host info", "ret", ret)
return ret
}
41 changes: 41 additions & 0 deletions common/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package common

type TAppParam struct {
ModuleType string
PairSessionId string
Robot3dId int
UE_Ip string
UE_Port int
UE_h5_weith int
UE_h5_higth int
}
type TAgentUserData struct {
SessionID string
CookieVal string
AppString string
ProxyBody interface{}
UserId string
}
type TWSData struct {
MsgType int
Msg []byte
}

const (
// MessageText is for UTF-8 encoded text messages like JSON.
MessageText = iota + 1
// MessageBinary is for binary messages like protobufs.
MessageBinary
// CloseMessage denotes a close control message. The optional message
// payload contains a numeric code and text. Use the FormatCloseMessage
// function to format a close message payload.
CloseMessage = 8

// PingMessage denotes a ping control message. The optional message payload
// is UTF-8 encoded text.
PingMessage = 9

// PongMessage denotes a pong control message. The optional message payload
// is UTF-8 encoded text.
PongMessage = 10
)
26 changes: 26 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package common

import (
"fmt"
"github.com/bwmarrin/snowflake"
log "github.com/xuexihuang/new_log15"
"runtime/debug"
)

// Method used to capture panic and print stack information
func TryRecoverAndDebugPrint() {
errs := recover()
if errs == nil {
return
}
fmt.Printf("panic: %+v\n%s", errs, debug.Stack())
log.Crit("[Panic]", "err", errs, "stackInfo", debug.Stack())

}

var G_flakeNode *snowflake.Node

func GetRandomSessionId() string {

return G_flakeNode.Generate().String()
}
15 changes: 15 additions & 0 deletions gate/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gate

import (
"net"
)

type Agent interface {
WriteMsg(msg interface{})
LocalAddr() net.Addr
RemoteAddr() net.Addr
Close()
Destroy()
UserData() interface{}
SetUserData(data interface{})
}
Loading

0 comments on commit 939678a

Please sign in to comment.