-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11bc0d7
commit 162716c
Showing
4 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# 示例 | ||
# 示例 | ||
|
||
简单的服务端和客户端实现 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/shiguanghuxian/tcplibrary" | ||
) | ||
|
||
func main() { | ||
log.SetFlags(log.Lshortfile | log.LstdFlags) | ||
|
||
for i := 0; i < 100; i++ { | ||
go func() { | ||
client := new(Client) | ||
c, err := tcplibrary.NewTCPClient(true, client) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
err = c.DialAndStart(":1028") | ||
log.Println(err) | ||
}() | ||
} | ||
|
||
select {} | ||
} | ||
|
||
// Client tcp客户端服务对象 | ||
type Client struct { | ||
} | ||
|
||
// OnConnect 连接建立时 | ||
func (c *Client) OnConnect(conn *tcplibrary.Conn) error { | ||
log.Println("OnConnect") | ||
go func() { | ||
conn := conn | ||
for { | ||
pp := &tcplibrary.DefaultPacket{ | ||
Payload: []byte("你好世界"), | ||
} | ||
n, err := conn.SendMessage(pp) | ||
log.Println(n, err) | ||
time.Sleep(1 * time.Second) | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
// OnError 遇到错误时 | ||
func (c *Client) OnError(err error) { | ||
log.Println("OnError") | ||
log.Println(err) | ||
} | ||
|
||
// OnClose 连接关闭时 | ||
func (c *Client) OnClose(conn *tcplibrary.Conn, err error) { | ||
log.Println("OnClose") | ||
log.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// OnRecMessage 收到消息时 | ||
func (c *Client) OnRecMessage(conn *tcplibrary.Conn, v interface{}) { | ||
log.Println("OnRecMessage") | ||
if packet, ok := v.(*tcplibrary.DefaultPacket); ok == true { | ||
log.Printf("消息体长度:%d 消息体内容:%s", packet.Length, string(packet.GetPayload())) | ||
} else { | ||
js, _ := json.Marshal(v) | ||
log.Println(string(js)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/shiguanghuxian/tcplibrary" | ||
) | ||
|
||
func main() { | ||
log.SetFlags(log.Lshortfile | log.LstdFlags) | ||
|
||
server := new(Server) | ||
// 启动websocket监听 | ||
webSocketServer, err := tcplibrary.NewWebSocketServer(true, server) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
go func() { | ||
err = webSocketServer.ListenAndServe(":1126", "/vivi") | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
|
||
// 启动tcp监听 | ||
tcpServer, err := tcplibrary.NewTCPServer(true, server) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
err = tcpServer.ListenAndServe(":1028") | ||
// 启动 | ||
log.Println(err) | ||
} | ||
|
||
// Server 服务端服务对象 | ||
type Server struct { | ||
} | ||
|
||
// OnConnect 连接建立时 | ||
func (s *Server) OnConnect(conn *tcplibrary.Conn) error { | ||
log.Println("OnConnect") | ||
return nil | ||
} | ||
|
||
// OnError 连接遇到错误时 | ||
func (s *Server) OnError(err error) { | ||
log.Println("OnError") | ||
log.Println(err) | ||
} | ||
|
||
// OnClose 连接关闭时 | ||
func (s *Server) OnClose(conn *tcplibrary.Conn, err error) { | ||
log.Println("OnClose") | ||
log.Println(err) | ||
} | ||
|
||
// OnRecMessage 收到客户端发送过来的消息时 | ||
func (s *Server) OnRecMessage(conn *tcplibrary.Conn, v interface{}) { | ||
log.Println("OnRecMessage") | ||
if packet, ok := v.(*tcplibrary.DefaultPacket); ok == true { | ||
log.Printf("消息体长度:%d 消息体内容:%s\n", packet.Length, string(packet.GetPayload())) | ||
// 转发给所有 | ||
n, err := tcplibrary.SendMessageToAll(v) | ||
log.Printf("成功发送%d个客户端,错误:%v\n", n, err) | ||
} else { | ||
js, _ := json.Marshal(v) | ||
log.Println(string(js)) | ||
} | ||
|
||
} | ||
|
||
var i = 0 | ||
|
||
// GetClientID 生成一个客户端连接,只要唯一即可 | ||
func (s *Server) GetClientID() string { | ||
i++ | ||
return fmt.Sprint(i) | ||
} |