# 客户端调用案例
> 自研聊天系统 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]))
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
读书笔记:golang聊天系统架构.zip (22个子文件)
读书笔记:golang聊天系统架构
go-imt
websocket.go 7KB
controllers.go 8KB
crypt.go 4KB
.git
index 864B
HEAD 23B
refs
heads
master 41B
tags
remotes
origin
master 41B
objects
pack
pack-1c487caea37c50e2a933f4c5d95aa94ec76b8ad5.idx 2KB
pack-1c487caea37c50e2a933f4c5d95aa94ec76b8ad5.pack 16KB
info
FETCH_HEAD 117B
logs
HEAD 130B
refs
heads
master 130B
remotes
origin
master 144B
hooks
config 254B
branches
httpsocket.go 3KB
router.go 2KB
imt.go 4KB
const.go 3KB
README.md 4KB
message.go 7KB
socket.go 2KB
function.go 2KB
共 22 条
- 1
资源评论
九转成圣
- 粉丝: 5556
- 资源: 2962
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功