# 客户端调用案例
> 自研聊天系统 http://www.openmessage.cn
```
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net"
"strings"
"time"
)
const (
MESSAGE_CHANNEL_END_BYTE byte = 0x03 //结尾符
MESSAGE_TYPES_SYSTEM_USER_ID = "system_user_id" //系统类型的通信,不作为用户展示信息
)
var UserUnique string
//用户请求资源内容
type MessageHeader struct {
UserUnique string `json:"userUnique"`
Data string `json:"data"`
}
func main () {
var Conn net.Conn
Conn,_ = net.Dial("tcp","127.0.0.1:1234")
var header,_ = json.Marshal(MessageHeader{
UserUnique: "",
Data: "杨凯",
})
header = append(header)
Conn.Write(append([]byte("TCP /Login SOCKET\r\n\r\n"),header...))
go func() {
for {
var buf = bufio.NewReader(Conn)
var message,err = buf.ReadString(MESSAGE_CHANNEL_END_BYTE)
message = strings.Trim(message,string(MESSAGE_CHANNEL_END_BYTE))
//解析读取数据结构
var messageS = struct {
Types string `json:"types"`
Data string `json:"data"`
}{}
json.Unmarshal([]byte(message),&messageS)
if err != nil || message == io.EOF.Error() {
Conn.Close()
break
}else{
switch messageS.Types {
case MESSAGE_TYPES_SYSTEM_USER_ID:UserUnique = messageS.Data;break
}
fmt.Println(messageS)
}
time.Sleep(time.Second*1)
}
}()
for {
var scan string
fmt.Scan(&scan)
switch scan {
case "callback":Callback();continue
case "getusers":GetUsers();continue
case "leave":Leave();continue
case "buildmessage":
fmt.Scan(&scan)
BuildMessage(scan);continue
case "sendmessage":
fmt.Scan(&scan)
SendMessage(scan);continue
default:
Conn,_ = net.Dial("tcp","127.0.0.1:1234")
var header,_ = json.Marshal(MessageHeader{
UserUnique: "",
Data: scan,
})
Conn.Write([]byte("TCP /SystemCtl SOCKET\r\n"))
Conn.Write(header)
var buf = bufio.NewReader(Conn)
var b = make([]byte,2048)
var n,_ = buf.Read(b)
fmt.Println(string(b[:n]))
}
}
}
func SendMessage(message string) {
var Conn net.Conn
Conn,_ = net.Dial("tcp","127.0.0.1:1234")
var header,_ = json.Marshal(MessageHeader{
UserUnique: UserUnique,
Data: message,
})
header = append(header)
Conn.Write(append([]byte("TCP /BothSend SOCKET\r\n\r\n"),header...))
var buf = bufio.NewReader(Conn)
var b = make([]byte,2048)
var n,_ = buf.Read(b)
fmt.Println(string(b[:n]))
}
func BuildMessage(userid string) {
var Conn net.Conn
Conn,_ = net.Dial("tcp","127.0.0.1:1234")
var header,_ = json.Marshal(MessageHeader{
UserUnique: UserUnique,
Data: userid,
})
header = append(header)
Conn.Write(append([]byte("TCP /BothAdd SOCKET\r\n\r\n"),header...))
var buf = bufio.NewReader(Conn)
var b = make([]byte,2048)
var n,_ = buf.Read(b)
fmt.Println(string(b[:n]))
}
func GetUsers() {
var Conn net.Conn
Conn,_ = net.Dial("tcp","127.0.0.1:1234")
var header,_ = json.Marshal(MessageHeader{
UserUnique: "",
Data: "",
})
header = append(header)
Conn.Write(append([]byte("TCP /OnlineUsers SOCKET\r\n\r\n"),header...))
var buf = bufio.NewReader(Conn)
var b = make([]byte,2048)
var n,_ = buf.Read(b)
fmt.Println(string(b[:n]))
}
func Leave() {
var Conn net.Conn
Conn,_ = net.Dial("tcp","127.0.0.1:1234")
var header,_ = json.Marshal(MessageHeader{
UserUnique: UserUnique,
Data: "",
})
header = append(header)
Conn.Write(append([]byte("TCP /Leave SOCKET\r\n\r\n"),header...))
var buf = bufio.NewReader(Conn)
var b = make([]byte,2048)
var n,_ = buf.Read(b)
fmt.Println(string(b[:n]))
}
func Callback() {
var Conn net.Conn
Conn,_ = net.Dial("tcp","127.0.0.1:1234")
var header,_ = json.Marshal(MessageHeader{
UserUnique: UserUnique,
Data: "测试发送的消息",
})
header = append(header)
Conn.Write(append([]byte("TCP /test SOCKET\r\n\r\n"),header...))
var buf = bufio.NewReader(Conn)
var b = make([]byte,2048)
var n,_ = buf.Read(b)
fmt.Println(string(b[:n]))
}
```
![avatar](https://profile-avatar.csdnimg.cn/bc3c40a1700d40bc91a04af0dc7133fa_weixin_41883161.jpg!1)
九转成圣
- 粉丝: 3605
- 资源: 2956
最新资源
- 变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图6#机专用盘图纸
- 变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图35KV开关柜微机综合保护原理图
- 变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图35-6~10KV变压器二次电路图
- 变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图35-0.38KV变压器二次电路图
- 变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图15.仪表电缆保护及连接图册
- 变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图12路电容柜接线图
- 变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图04FD02防空地下室电气设备安装
- NVIDIA GeForce GTX 1050 Ti 驱动程序
- 变压器变频器配电柜电路控制原理图CAD施工图纸设备控制图总降压站继电图
- 20240909-140253 python 应用题 文件数据读取计算
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)