Skip to content

Commit

Permalink
添加示例
Browse files Browse the repository at this point in the history
  • Loading branch information
shiguanghuxian committed Dec 30, 2017
1 parent 11bc0d7 commit 162716c
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# 示例
# 示例

简单的服务端和客户端实现
3 changes: 0 additions & 3 deletions examples/chat/README.md

This file was deleted.

73 changes: 73 additions & 0 deletions examples/echo-client/main.go
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))
}
}
80 changes: 80 additions & 0 deletions examples/echo-server/main.go
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)
}

0 comments on commit 162716c

Please sign in to comment.