Commit 0bbe94eb authored by haoyanbin's avatar haoyanbin

1

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