Commit 0bbe94eb authored by haoyanbin's avatar haoyanbin

1

parent 4d5d4553
......@@ -60,20 +60,20 @@ type RuntimeInfo struct {
}
type SendMsg struct {
MsgId string `json:"msgId"`
SendTime string `json:"sendTime"` //消息发送时间
FromClientId string `json:"fromClientId"` //指令消息的来源。发送者的连接ID
ToClientId string `json:"toClientId"` //指令消息的接收者。发送给对应的客户端连接ID
CmdData []byte `json:"cmdData"` //对应指令的CmdData1的protobuf的message
Status int `json:"status"` //消息发送响应状态
Priority int `json:"priority"` //用于处理指令队列的优先级的权重值
BusinessId string `json:"businessId"` //客户端标识消息的id,主要区分相同cmd的不同消息,方便收到回复分发处理等效果,考虑长度问题定义成string
GroupId string `json:"groupId"` //服务端发送消息的ID,主要区分相同cmd的不同消息,方便服务端收到回复分发处理等效果,考虑长度问题定义成string
Channel []string `json:"channel"` //指定需要广播的频道,可以指定一个或多个频道
ProcedureType int `json:"procedureType"` //会话状态 1 开始导诊 2 等待连接 3 离线 4 双方建立连接 5 结束会话 6 对话 7 已读 8 超时关闭
MsgType int `json:"msgType"` // msg类型 procedureType=6时填写 1 文本消息 2 图片消息
Msg string `json:"msg"` //一般用于json数据传递,或消息发送响应内容
Desc string `json:"desc"` //消息介绍内容,或其它数据
ConversationId int `json:"conversationId"`
SendTime string `json:"sendTime"` //消息发送时间
FromClientId string `json:"fromClientId"` //指令消息的来源。发送者的连接ID
ToClientId string `json:"toClientId"` //指令消息的接收者。发送给对应的客户端连接ID
CmdData []byte `json:"cmdData"` //对应指令的CmdData1的protobuf的message
Status int `json:"status"` //消息发送响应状态
Priority int `json:"priority"` //用于处理指令队列的优先级的权重值
BusinessId string `json:"businessId"` //客户端标识消息的id,主要区分相同cmd的不同消息,方便收到回复分发处理等效果,考虑长度问题定义成string
GroupId string `json:"groupId"` //服务端发送消息的ID,主要区分相同cmd的不同消息,方便服务端收到回复分发处理等效果,考虑长度问题定义成string
Channel []string `json:"channel"` //指定需要广播的频道,可以指定一个或多个频道
ProcedureType int `json:"procedureType"` //会话状态 1 开始导诊 2 等待连接 3 离线 4 双方建立连接 5 结束会话 6 对话 7 已读 8 超时关闭
MsgType int `json:"msgType"` // msg类型 procedureType=6时填写 1 文本消息 2 图片消息
Msg string `json:"msg"` //一般用于json数据传递,或消息发送响应内容
Desc string `json:"desc"` //消息介绍内容,或其它数据
}
// Client is a middleman between the websocket connection and the hub.
......@@ -114,8 +114,9 @@ type Client struct {
//}
type Conversation struct {
CustomerClientId string `json:"customerClientId"`
UserClientId string `json:"userClientId"`
ConversationId int `json:"conversationId"`
Promoter string `json:"promoter"`
Participant string `json:"participant"`
StartTime string `json:"startTime"`
StartReceiveDate string `json:"startReceiveDate"`
Status int `json:"status"`
......
......@@ -5,42 +5,30 @@ import (
"fmt"
"net/http"
"pool/pool/util/grpool"
"strconv"
"strings"
"sync"
"time"
)
type SetMsgReq struct {
ProcedureType int `json:"procedureType"`
GroupId string `json:"groupId" db:"group_id"`
UserId string `json:"userId" db:"user_id"`
BusinessId string `json:"businessId" db:"business_id"`
CustomerId string `json:"customerId" db:"customer_id"`
Status string `json:"status" db:"status"`
StartTime string `json:"startTime" db:"start_time"`
EndTime string `json:"endTime" db:"end_time"`
Remark string `json:"remark" db:"remark"`
Promoter string `json:"promoter" db:"promoter"`
Finish string `json:"finish" db:"finish"`
ExpertUnread string `json:"expertUnread" db:"expert_unread"`
UserUnread string `json:"userUnread" db:"user_unread"`
ExpertUnreadMessage string `json:"expertUnreadMessage" db:"expert_unread_message"`
ExpertUnreadMessageTime string `json:"expertUnreadMessageTime" db:"expert_unread_message_time"`
GuideMsg string `json:"guideMsg" db:"guide_msg"`
GuideDate string `json:"guideDate" db:"guide_date"`
ReceiveDate string `json:"receiveDate" db:"receive_date"`
ConversationDate string `json:"conversationDate" db:"conversation_date"`
StartReceiveDate string `json:"startReceiveDate" db:"start_receive_date"`
ConversationId string `json:"conversationId" db:"conversation_id"`
PromoterType string `json:"promoterType" db:"promoter_type"`
SendTime string `json:"sendTime" db:"send_time"`
MsgType int `json:"msgType" db:"msg_type"`
Content string `json:"content" db:"content"`
Sender string `json:"sender" db:"sender"`
Receiver string `json:"receiver" db:"receiver"`
HandleTime string `json:"handleTime" db:"handle_time"`
HandlePersonId string `json:"handlePersonId" db:"handle_person_id"`
HandlePerson string `json:"handlePerson" db:"handle_person"`
ProcedureType int `json:"procedureType"`
ConversationId int `json:"conversationId" db:"conversation_id"`
Promoter string `json:"promoter" db:"promoter"`
Participant string `json:"participant" db:"participant"`
Status string `json:"status" db:"status"`
StartTime string `json:"startTime" db:"start_time"`
EndTime string `json:"endTime" db:"end_time"`
Remark string `json:"remark" db:"remark"`
Finish string `json:"finish" db:"finish"`
GuideMsg string `json:"guideMsg" db:"guide_msg"`
GuideDate string `json:"guideDate" db:"guide_date"`
StartReceiveDate string `json:"startReceiveDate" db:"start_receive_date"`
SendTime string `json:"sendTime" db:"send_time"`
MsgType int `json:"msgType" db:"msg_type"`
Content string `json:"content" db:"content"`
Sender string `json:"sender" db:"sender"`
Receiver string `json:"receiver" db:"receiver"`
}
// 第一步,实例化连接对像
......@@ -306,13 +294,13 @@ func BroadcastAll(msg *SendMsg) error {
}
// 根据来源和类型获取客户端列表
func GetList(source, userType string) map[string]*Client {
func GetList(source, sourceId, customerType string) map[string]*Client {
clientData := make(map[string]*Client, 0)
for k, v := range wsSever.hub.clients {
userInfo := strings.Split(k, "_")
if (userInfo[0] == source) || (source == "") {
if userType != "" {
if userInfo[1] == userType {
if (userInfo[0] == source && userInfo[1] == sourceId) || (source == "") {
if customerType != "" {
if userInfo[1] == customerType {
clientData[k] = v
}
} else {
......@@ -324,11 +312,12 @@ func GetList(source, userType string) map[string]*Client {
}
type UserInfo struct {
Source string `json:"source"` //
Promoter string `json:"promoter"`
CustomerId string `json:"customerId"`
ClientId string `json:"clientId"`
ToChannel []string `json:"toChannel"`
Source string `json:"source"` //
SourceId string `json:"sourceId"` //
CustomerType string `json:"customerType"`
CustomerId string `json:"customerId"`
ClientId string `json:"clientId"`
ToChannel []string `json:"toChannel"`
}
//根据token获取用户来源信息
......@@ -336,7 +325,7 @@ func GetClientInfoByToken(token string) (*UserInfo, error) {
tokenData := strings.Split(token, "_")
if len(tokenData) < 3 {
if len(tokenData) < 4 {
return nil, errors.New("用户数据有误")
}
......@@ -353,55 +342,23 @@ func GetClientInfoByToken(token string) (*UserInfo, error) {
userData := new(UserInfo)
userData.Source = tokenData[0]
userData.Promoter = tokenData[1]
userData.CustomerId = tokenData[2]
userData.SourceId = tokenData[1]
userData.CustomerType = tokenData[2]
userData.CustomerId = tokenData[3]
userData.ClientId = userData.Source + "_" + userData.Promoter + "_" + userData.CustomerId
userData.ClientId = userData.Source + "_" + userData.Source + "_" + userData.CustomerType + "_" + userData.CustomerId
return userData, nil
}
// 会话状态 1 开始导诊 2 等待连接 3 双方建立连接 4 结束会话 5 离线 6 对话 7 已读 8 超时自动结束
func SaveMsg(msg *SendMsg) {
msg.SendTime = time.Now().Format("2006-01-02 15:04:05")
user := GetClientInfoById(msg.FromClientId)
toUser := &UserInfo{}
if msg.ToClientId != "" {
toUser = GetClientInfoById(msg.ToClientId)
}
if user.Promoter == "" {
fmt.Println("用户数据有误")
return
}
mqData := &SetMsgReq{}
if user.Promoter == "1" {
mqData.BusinessId = user.Source
mqData.CustomerId = user.CustomerId
mqData.GroupId = toUser.Source
mqData.UserId = toUser.CustomerId
}
if user.Promoter == "2" {
mqData.BusinessId = toUser.Source
mqData.CustomerId = toUser.CustomerId
mqData.GroupId = user.Source
mqData.UserId = user.CustomerId
}
if mqData.BusinessId != "" {
//mqData.BusinessId, _ = DataAesDecrypt(mqData.BusinessId)
mqData.BusinessId = mqData.BusinessId
}
mqData.ProcedureType = msg.ProcedureType
if msg.ProcedureType == 1 {
mqData.StartTime = msg.SendTime
mqData.Promoter = user.Promoter
}
mqData.ConversationId = msg.ConversationId
if msg.ProcedureType == 2 {
mqData.GuideMsg = msg.Msg
......@@ -411,17 +368,13 @@ func SaveMsg(msg *SendMsg) {
//建立连接
if msg.ProcedureType == 3 {
mqData.StartReceiveDate = msg.SendTime
mqData.Participant = msg.FromClientId
//连接开启信息存入
conversation := &Conversation{}
if user.Promoter == "1" {
conversation.CustomerClientId = msg.FromClientId
conversation.UserClientId = msg.ToClientId
}
if user.Promoter == "2" {
conversation.CustomerClientId = msg.ToClientId
conversation.UserClientId = msg.FromClientId
}
conversation.ConversationId = msg.ConversationId
conversation.Promoter = msg.FromClientId
conversation.Participant = msg.ToClientId
conversation.Status = 3
conversation.StartReceiveDate = mqData.StartReceiveDate
AppendConversation(conversation)
......@@ -430,29 +383,28 @@ func SaveMsg(msg *SendMsg) {
//结束
if msg.ProcedureType == 4 {
mqData.EndTime = msg.SendTime
mqData.Promoter = user.Promoter
if user.Promoter == "1" {
if user.CustomerType == "1" {
mqData.Finish = "3"
}
if user.Promoter == "2" {
if user.CustomerType == "2" {
mqData.Finish = "1"
}
}
//离线
if msg.ProcedureType == 5 {
if user.Promoter == "1" {
if user.CustomerType == "1" {
mqData.Finish = "1"
}
if user.Promoter == "2" {
if user.CustomerType == "2" {
mqData.Finish = "2"
}
}
//对话
if msg.ProcedureType == 6 {
mqData.PromoterType = user.Promoter
mqData.SendTime = msg.SendTime
mqData.MsgType = msg.MsgType
mqData.Content = msg.Msg
......@@ -461,21 +413,14 @@ func SaveMsg(msg *SendMsg) {
}
//已读状态
if msg.ProcedureType == 7 {
if user.Promoter == "1" {
mqData.PromoterType = "2"
}
if user.Promoter == "2" {
mqData.PromoterType = "1"
}
mqData.SendTime = msg.SendTime
}
//if msg.ProcedureType == 7 {
// mqData.SendTime = msg.SendTime
//}
if msg.ProcedureType == 8 {
mqData.EndTime = msg.SendTime
mqData.Promoter = user.Promoter
mqData.Finish = "5"
DelConversation(msg.FromClientId, msg.ToClientId)
DelConversation(msg.ConversationId)
}
if mqData.ProcedureType != 0 {
......@@ -490,14 +435,15 @@ func GetClientInfoById(clientId string) *UserInfo {
userData := new(UserInfo)
if len(data) < 3 {
if len(data) < 4 {
fmt.Println("用户数据有误")
return userData
}
userData.Source = data[0]
userData.Promoter = data[1]
userData.CustomerId = data[2]
userData.SourceId = data[1]
userData.CustomerType = data[2]
userData.CustomerId = data[3]
return userData
}
......@@ -514,12 +460,12 @@ func GetAllList(key string) map[string]string {
}
func AppendConversation(conversation *Conversation) {
Redis.HSetNX(convKey, conversation.CustomerClientId+"^"+conversation.UserClientId, string(SerializeJson(conversation)))
Redis.HSetNX(convKey, strconv.Itoa(conversation.ConversationId), string(SerializeJson(conversation)))
return
}
func DelConversation(customerClientId, userClientId string) {
Redis.HDel(convKey, customerClientId+"^"+userClientId)
func DelConversation(conversationId int) {
Redis.HDel(convKey, strconv.Itoa(conversationId))
return
}
......@@ -541,25 +487,25 @@ func SetEnd() {
if conversationData.StartReceiveDate < time.Now().Add(-endDate).Format("2006-01-02 15:04:05") {
mqData := &SendMsg{
ProcedureType: 8,
ToClientId: conversationData.CustomerClientId,
FromClientId: conversationData.UserClientId,
ToClientId: conversationData.Promoter,
FromClientId: conversationData.Participant,
SendTime: time.Now().Format("2006-01-02 15:04:05"),
}
//发送结束会话给客户端
_, isSet := wsSever.hub.clients[conversationData.CustomerClientId]
_, isSet := wsSever.hub.clients[conversationData.Promoter]
if isSet == true {
wsSever.hub.clients[conversationData.CustomerClientId].readMessage(mqData)
wsSever.hub.clients[conversationData.Promoter].readMessage(mqData)
}
toMqData := &SendMsg{
ProcedureType: 8,
ToClientId: conversationData.UserClientId,
FromClientId: conversationData.CustomerClientId,
ToClientId: conversationData.Participant,
FromClientId: conversationData.Promoter,
SendTime: time.Now().Format("2006-01-02 15:04:05"),
}
_, isSet = wsSever.hub.clients[conversationData.UserClientId]
_, isSet = wsSever.hub.clients[conversationData.Participant]
if isSet == true {
wsSever.hub.clients[conversationData.UserClientId].readMessage(toMqData)
wsSever.hub.clients[conversationData.Participant].readMessage(toMqData)
}
SaveMsg(toMqData)
}
......
......@@ -97,12 +97,6 @@ func ws(w http.ResponseWriter, r *http.Request) {
//接收消息
client.OnMessage(func(msg *pool.SendMsg) {
user := pool.GetClientInfoById(client.Id)
if user.Promoter == "1" {
msg.BusinessId = user.Source
} else if user.Promoter == "2" {
msg.GroupId = user.Source
}
if msg.Status == 3 {
fmt.Println("OnMessage:收到出错消息=》", client.Id, msg.Desc)
......@@ -143,24 +137,17 @@ func ws(w http.ResponseWriter, r *http.Request) {
closeMsg := &pool.SetMsgReq{}
closeMsg.ProcedureType = 5
closeMsg.EndTime = time.Now().Format("2006-01-02 15:04:05")
closeMsg.Promoter = user.Promoter
if user.Promoter == "1" {
closeMsg.Finish = "3"
if user.CustomerType == "1" {
closeMsg.Promoter = client.Id
if userInfo.Source != "" {
closeMsg.BusinessId, _ = pool.DataAesDecrypt(userInfo.Source)
}
closeMsg.CustomerId = userInfo.CustomerId
pool.PublishData(closeMsg)
fmt.Println("用户关闭连接", client.Id)
}
if user.Promoter == "2" {
closeMsg.Finish = "4"
closeMsg.GroupId = userInfo.Source
closeMsg.UserId = userInfo.CustomerId
if user.CustomerType == "2" {
closeMsg.Participant = client.Id
pool.PublishData(closeMsg)
fmt.Println("专家关闭连接", client.Id)
}
......@@ -182,17 +169,17 @@ func ws(w http.ResponseWriter, r *http.Request) {
func GetClientList(w http.ResponseWriter, r *http.Request) {
data := r.URL.Query()
list := pool.GetList(data["source"][0], data["promoter"][0])
list := pool.GetList(data["source"][0], data["sourceId"][0], data["customerType"][0])
reply := make([]string, 0)
for k := range list {
reply = append(reply, k)
}
resp(w,reply)
resp(w, reply)
}
func resp(w http.ResponseWriter,data interface{}){
func resp(w http.ResponseWriter, data interface{}) {
w.Header().Set("content-type", "text/json")
w.WriteHeader(200)
w.Write(pool.SerializeJson(data))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment