-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from xuexihuang/main
Implement basic functions
- Loading branch information
Showing
26 changed files
with
1,504 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
Oops, something went wrong.