聊天界面和获取在线用户接口

pull/30/head
taoshihan1991 5 years ago
parent c2fd9ebad1
commit 47c90e4566

@ -1,11 +1,125 @@
package controller
import (
"encoding/json"
"github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools"
"golang.org/x/net/websocket"
"log"
"net/http"
"time"
)
//聊天主界面
func ActionChatMain(w http.ResponseWriter, r *http.Request){
render:=tmpl.NewRender(w)
render.Display("chat_main",nil)
}
//获取在线用户
func ChatUsers(w http.ResponseWriter, r *http.Request){
w.Header().Set("content-type", "text/json;charset=utf-8;")
result:=make([]map[string]string,0)
for uid,_:=range clientList{
userInfo:=make(map[string]string)
userInfo["uid"]=uid
result=append(result,userInfo)
}
msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result,
})
w.Write(msg)
}
//兼容之前的聊天服务
func ChatServer(w *websocket.Conn){
var error error
for {
//接受消息
var receive string
if error = websocket.Message.Receive(w, &receive); error != nil {
log.Println("接受消息失败", error)
break
}
message := Message{}
err := json.Unmarshal([]byte(receive), &message)
if err != nil {
log.Println(err)
}
chat := ChatUserMessage{}
json.Unmarshal([]byte(receive), &chat)
log.Println("客户端:", message)
kfMessageData := KfMessageData{
Kf_name: "客服小美",
Avatar: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4217138672,2588039002&fm=26&gp=0.jpg",
Kf_id: "KF2",
Time: time.Now().Format("2006-01-02 15:04:05"),
Content: chat.Data.Content,
}
switch message.Type.(string) {
//用户初始化
case "userInit":
clientList[message.Uid.(string)] = w
sendMsg := ChatKfMessage{
Message_type: "kf_online",
Data: kfMessageData,
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
//正常发送消息
case "chatMessage":
sendMsg := ChatKfMessage{
Message_type: "chatMessage",
Data: kfMessageData,
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
//回应ping
case "ping":
sendMsg := PingMessage{
Type: "pong",
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
}
}
}
type Message struct {
Type interface{}
Uid interface{}
Name interface{}
Avatar interface{}
Group interface{}
}
type KfMessageData struct {
Kf_name interface{} `json:"kf_name"`
Avatar interface{} `json:"avatar"`
Kf_id interface{} `json:"kf_id"`
Time interface{} `json:"time"`
Content interface{} `json:"content"`
}
type UserMessageData struct {
From_avatar interface{} `json:"from_avatar"`
From_id interface{} `json:"from_id"`
From_name interface{} `json:"from_name"`
To_id interface{} `json:"to_id"`
To_name interface{} `json:"to_name"`
Content interface{} `json:"content"`
}
type ChatKfMessage struct {
Message_type interface{} `json:"message_type"`
Data KfMessageData `json:"data"`
}
type ChatUserMessage struct {
Message_type interface{} `json:"message_type"`
Data UserMessageData `json:"data"`
}
type PingMessage struct {
Type interface{} `json:"type"`
}
var clientList =make(map[string]*websocket.Conn)

@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"github.com/taoshihan1991/imaptool/controller"
"golang.org/x/net/websocket"
"log"
@ -43,7 +42,9 @@ func main() {
//新邮件提醒服务
mux.HandleFunc("/push_mail", controller.PushMailServer)
//聊天服务
mux.Handle("/chat_server",websocket.Handler(ChatServer))
mux.Handle("/chat_server",websocket.Handler(controller.ChatServer))
//获取在线用户
mux.HandleFunc("/chat_users", controller.ChatUsers)
//监听端口
//http.ListenAndServe(":8080", nil)
//var myHandler http.Handler
@ -56,97 +57,3 @@ func main() {
}
s.ListenAndServe()
}
//兼容之前的聊天服务
func ChatServer(w *websocket.Conn){
var error error
for {
//接受消息
var receive string
if error = websocket.Message.Receive(w, &receive); error != nil {
log.Println("接受消息失败", error)
break
}
message := Message{}
err := json.Unmarshal([]byte(receive), &message)
if err != nil {
log.Println(err)
}
chat := ChatUserMessage{}
json.Unmarshal([]byte(receive), &chat)
log.Println("客户端:", message)
kfMessageData := KfMessageData{
Kf_name: "客服小美",
Avatar: "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4217138672,2588039002&fm=26&gp=0.jpg",
Kf_id: "KF2",
Time: time.Now().Format("2006-01-02 15:04:05"),
Content: chat.Data.Content,
}
switch message.Type.(string) {
//用户初始化
case "userInit":
clientList[message.Uid.(string)] = w
sendMsg := ChatKfMessage{
Message_type: "kf_online",
Data: kfMessageData,
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
//正常发送消息
case "chatMessage":
sendMsg := ChatKfMessage{
Message_type: "chatMessage",
Data: kfMessageData,
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
//回应ping
case "ping":
sendMsg := PingMessage{
Type: "pong",
}
jsonStrByte, _ := json.Marshal(sendMsg)
log.Println("服务端:", string(jsonStrByte))
websocket.Message.Send(w, string(jsonStrByte))
}
}
}
type Message struct {
Type interface{}
Uid interface{}
Name interface{}
Avatar interface{}
Group interface{}
}
type KfMessageData struct {
Kf_name interface{} `json:"kf_name"`
Avatar interface{} `json:"avatar"`
Kf_id interface{} `json:"kf_id"`
Time interface{} `json:"time"`
Content interface{} `json:"content"`
}
type UserMessageData struct {
From_avatar interface{} `json:"from_avatar"`
From_id interface{} `json:"from_id"`
From_name interface{} `json:"from_name"`
To_id interface{} `json:"to_id"`
To_name interface{} `json:"to_name"`
Content interface{} `json:"content"`
}
type ChatKfMessage struct {
Message_type interface{} `json:"message_type"`
Data KfMessageData `json:"data"`
}
type ChatUserMessage struct {
Message_type interface{} `json:"message_type"`
Data UserMessageData `json:"data"`
}
type PingMessage struct {
Type interface{} `json:"type"`
}
var clientList =make(map[string]*websocket.Conn)

@ -12,8 +12,43 @@
<style>
html, body {height: 100%;padding: 0;margin: 0;background-color: #f5f5f5;}
.el-row{width:100%}
.chatLeft{border-bottom: solid 1px #e6e6e6}
.sw-bg{background: #fff;border: solid 1px #e6e6e6;boder-top:none;padding:5px 10px;}
.chatContext{background: #fff}
.chatContext .el-row{margin-bottom: 5px;}
.chatUser{
line-height: 24px;
font-size: 12px;
white-space: nowrap;
color: #999;
}
.chatContent{
text-align: left;
background-color: rgb(166,212,242);
color: #000;
border: 1px solid rgb(152, 199, 230);
padding: 8px 15px;
min-height: 35px;
word-break: break-all;
position: relative;
}
.chatContent:after {
content: '';
position: absolute;
left: -10px;
top: 13px;
width: 0;
height: 0;
border-style: dashed;
border-color: transparent;
overflow: hidden;
border-width: 10px;
border-top-style: solid;
border-top-color: rgb(166,212,242);
}
.chatBoxMe .el-col-3{float: right;text-align: right;}
.chatBoxMe .chatUser{text-align: right}
.chatBoxMe .chatContent:after{left:auto;right: -10px;}
.chatArea{margin: 10px 0;}
</style>
</head>
<body>
@ -21,7 +56,7 @@
<template>
<el-row :gutter="5">
<el-col :span="6">
<el-menu>
<el-menu class="chatLeft">
<el-menu-item>
<i class="el-icon-user"></i>
<span slot="title">访客1112</span>
@ -29,16 +64,22 @@
</el-menu>
</el-col>
<el-col :span="12">
<div class="sw-bg">
<div class="sw-bg chatContext">
<el-row :gutter="2">
<el-col :span="4"><el-avatar :size="60" :src="circleUrl"></el-avatar></el-col>
<el-col :span="20">内容</el-col>
<el-col :span="3"><el-avatar :size="60" :src="circleUrl"></el-avatar></el-col>
<el-col :span="21">
<div class="chatUser">内容</div>
<div class="chatContent">内容</div>
</el-col>
</el-row>
<el-row :gutter="2">
<el-col :span="20">内容</el-col>
<el-col :span="4"><el-avatar :size="60" :src="circleUrl"></el-avatar></el-col>
<el-row :gutter="2" class="chatBoxMe">
<el-col :span="3"><el-avatar :size="60" :src="circleUrl"></el-avatar></el-col>
<el-col :span="21">
<div class="chatUser">内容</div>
<div class="chatContent">内容</div>
</el-col>
</el-row>
<el-input type="textarea"></el-input>
<el-input type="textarea" class="chatArea"></el-input>
<el-button type="primary">发送</el-button>
</div>
</el-col>

Loading…
Cancel
Save