Commit 9de187bb authored by alexstocks's avatar alexstocks

add ws client

parent 70d1d8bc
...@@ -12,7 +12,6 @@ package main ...@@ -12,7 +12,6 @@ package main
import ( import (
"math/rand" "math/rand"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
...@@ -53,7 +52,7 @@ func (this *EchoClient) close() { ...@@ -53,7 +52,7 @@ func (this *EchoClient) close() {
if client.gettyClient != nil { if client.gettyClient != nil {
for _, s := range this.sessions { for _, s := range this.sessions {
log.Info("close client session{%s, last active:%s, request number:%d}", log.Info("close client session{%s, last active:%s, request number:%d}",
s.session.Stat(), s.active.String(), s.reqNum) s.session.Stat(), s.session.GetActive().String(), s.reqNum)
s.session.Close() s.session.Close()
} }
client.gettyClient.Close() client.gettyClient.Close()
...@@ -83,7 +82,7 @@ func (this *EchoClient) addSession(session *getty.Session) { ...@@ -83,7 +82,7 @@ func (this *EchoClient) addSession(session *getty.Session) {
} }
this.lock.Lock() this.lock.Lock()
this.sessions = append(this.sessions, &clientEchoSession{session: session, active: time.Now()}) this.sessions = append(this.sessions, &clientEchoSession{session: session})
this.lock.Unlock() this.lock.Unlock()
} }
...@@ -115,7 +114,6 @@ func (this *EchoClient) updateSession(session *getty.Session) { ...@@ -115,7 +114,6 @@ func (this *EchoClient) updateSession(session *getty.Session) {
for i, s := range this.sessions { for i, s := range this.sessions {
if s.session == session { if s.session == session {
this.sessions[i].active = time.Now()
this.sessions[i].reqNum++ this.sessions[i].reqNum++
break break
} }
...@@ -145,21 +143,3 @@ func (this *EchoClient) getClientEchoSession(session *getty.Session) (clientEcho ...@@ -145,21 +143,3 @@ func (this *EchoClient) getClientEchoSession(session *getty.Session) (clientEcho
return echoSession, err return echoSession, err
} }
func (this *EchoClient) heartbeat(session *getty.Session) {
var pkg EchoPackage
pkg.H.Magic = echoPkgMagic
pkg.H.LogID = (uint32)(src.Int63())
pkg.H.Sequence = atomic.AddUint32(&reqID, 1)
// pkg.H.ServiceID = 0
pkg.H.Command = heartbeatCmd
pkg.B = echoHeartbeatRequestString
pkg.H.Len = (uint16)(len(pkg.B))
if err := session.WritePkg(&pkg); err != nil {
log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
session.Close()
this.removeSession(session)
}
}
...@@ -57,6 +57,7 @@ type ( ...@@ -57,6 +57,7 @@ type (
// server // server
ServerHost string `default:"127.0.0.1"` ServerHost string `default:"127.0.0.1"`
ServerPort int `default:"10000"` ServerPort int `default:"10000"`
ServerPath string `default:"/echo"`
ProfilePort int `default:"10086"` ProfilePort int `default:"10086"`
// session pool // session pool
......
/****************************************************** /******************************************************
# DESC : getty utility # DESC : echo package
# AUTHOR : Alex Stocks # AUTHOR : Alex Stocks
# LICENCE : Apache License 2.0 # LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com # EMAIL : alexstocks@foxmail.com
# MOD : 2016-08-22 17:44 # MOD : 2016-08-22 17:44
# FILE : utils.go # FILE : echo.go
******************************************************/ ******************************************************/
package main package main
...@@ -28,12 +28,10 @@ import ( ...@@ -28,12 +28,10 @@ import (
type echoCommand uint32 type echoCommand uint32
const ( const (
heartbeatCmd = iota echoCmd = iota
echoCmd
) )
var echoCommandStrings = [...]string{ var echoCommandStrings = [...]string{
"heartbeat",
"echo", "echo",
} }
...@@ -48,9 +46,6 @@ func (c echoCommand) String() string { ...@@ -48,9 +46,6 @@ func (c echoCommand) String() string {
const ( const (
echoPkgMagic = 0x20160905 echoPkgMagic = 0x20160905
maxEchoStringLen = 0xff maxEchoStringLen = 0xff
echoHeartbeatRequestString = "ping"
echoHeartbeatResponseString = "pong"
) )
var ( var (
...@@ -131,7 +126,7 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) { ...@@ -131,7 +126,7 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
if buf.Len() < (int)(this.H.Len) { if buf.Len() < (int)(this.H.Len) {
return 0, ErrNotEnoughSteam return 0, ErrNotEnoughSteam
} }
if maxEchoStringLen < this.H.Len { if maxEchoStringLen < this.H.Len-1 {
return 0, ErrTooLargePackage return 0, ErrTooLargePackage
} }
...@@ -141,5 +136,5 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) { ...@@ -141,5 +136,5 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
} }
this.B = (string)(buf.Next((int)(len))) this.B = (string)(buf.Next((int)(len)))
return (int)(this.H.Len) + 1 + echoPkgHeaderLen, nil return (int)(this.H.Len) + echoPkgHeaderLen, nil
} }
...@@ -29,7 +29,6 @@ var ( ...@@ -29,7 +29,6 @@ var (
type clientEchoSession struct { type clientEchoSession struct {
session *getty.Session session *getty.Session
active time.Time
reqNum int32 reqNum int32
} }
...@@ -73,12 +72,10 @@ func (this *EchoMessageHandler) OnCron(session *getty.Session) { ...@@ -73,12 +72,10 @@ func (this *EchoMessageHandler) OnCron(session *getty.Session) {
log.Error("client.getClientSession(session{%s}) = error{%#v}", session.Stat(), err) log.Error("client.getClientSession(session{%s}) = error{%#v}", session.Stat(), err)
return return
} }
if conf.sessionTimeout.Nanoseconds() < time.Since(clientEchoSession.active).Nanoseconds() { if conf.sessionTimeout.Nanoseconds() < time.Since(session.GetActive()).Nanoseconds() {
log.Warn("session{%s} timeout{%s}, reqNum{%d}", log.Warn("session{%s} timeout{%s}, reqNum{%d}",
session.Stat(), time.Since(clientEchoSession.active).String(), clientEchoSession.reqNum) session.Stat(), time.Since(session.GetActive()).String(), clientEchoSession.reqNum)
client.removeSession(session) client.removeSession(session)
return return
} }
client.heartbeat(session)
} }
...@@ -102,7 +102,7 @@ func initClient() { ...@@ -102,7 +102,7 @@ func initClient() {
client.gettyClient = getty.NewClient( client.gettyClient = getty.NewClient(
(int)(conf.ConnectionNum), (int)(conf.ConnectionNum),
conf.connectInterval, conf.connectInterval,
gxnet.HostAddress(conf.ServerHost, conf.ServerPort), gxnet.WSHostAddress(conf.ServerHost, conf.ServerPort, conf.ServerPath),
) )
client.gettyClient.RunEventLoop(newSession) client.gettyClient.RunEventLoop(newSession)
} }
...@@ -146,7 +146,7 @@ func echo() { ...@@ -146,7 +146,7 @@ func echo() {
// pkg.H.ServiceID = 0 // pkg.H.ServiceID = 0
pkg.H.Command = echoCmd pkg.H.Command = echoCmd
pkg.B = conf.EchoString pkg.B = conf.EchoString
pkg.H.Len = (uint16)(len(pkg.B)) pkg.H.Len = (uint16)(len(pkg.B)) + 1
if session := client.selectSession(); session != nil { if session := client.selectSession(); session != nil {
err := session.WritePkg(&pkg) err := session.WritePkg(&pkg)
......
...@@ -7,8 +7,9 @@ AppName = "ECHO-CLIENT" ...@@ -7,8 +7,9 @@ AppName = "ECHO-CLIENT"
LocalHost = "127.0.0.1" LocalHost = "127.0.0.1"
# server # server
ServerHost = "192.168.35.1" ServerHost = "192.168.35.3"
ServerPort = 10000 ServerPort = 10000
ServerPath = "/echo"
ProfilePort = 10080 ProfilePort = 10080
# connection pool # connection pool
......
<logging> <logging>
<filter enabled="false"> <filter enabled="true">
<tag>stdout</tag> <tag>stdout</tag>
<type>console</type> <type>console</type>
<!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) --> <!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->
......
...@@ -8,8 +8,7 @@ $(function() { ...@@ -8,8 +8,7 @@ $(function() {
]; ];
var seq = 0; var seq = 0;
var echoCommand = 0x01; var echoCommand = 0x00;
var heartbeatCommand = 0x02;
/** /**
* create struct * create struct
...@@ -222,9 +221,11 @@ $(function() { ...@@ -222,9 +221,11 @@ $(function() {
fileReader.readAsArrayBuffer(e.data); // 此处读取blob fileReader.readAsArrayBuffer(e.data); // 此处读取blob
} }
socket.onclose = function() { socket.onclose = function(e) {
// console.log("socket.onclose" + e.reason)
disconnect()
addChatMessage({ addChatMessage({
Message: '!!SYSTEM-WS-Close, connection closed' Message: e.reason + '!!SYSTEM-WS-Close, connection closed'
}); });
} }
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# LICENCE : Apache License 2.0 # LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com # EMAIL : alexstocks@foxmail.com
# MOD : 2016-08-22 17:44 # MOD : 2016-08-22 17:44
# FILE : utils.go # FILE : echo.go
******************************************************/ ******************************************************/
package main package main
...@@ -28,12 +28,10 @@ import ( ...@@ -28,12 +28,10 @@ import (
type echoCommand uint32 type echoCommand uint32
const ( const (
heartbeatCmd = iota echoCmd = iota
echoCmd
) )
var echoCommandStrings = [...]string{ var echoCommandStrings = [...]string{
"heartbeat",
"echo", "echo",
} }
...@@ -48,10 +46,6 @@ func (c echoCommand) String() string { ...@@ -48,10 +46,6 @@ func (c echoCommand) String() string {
const ( const (
echoPkgMagic = 0x20160905 echoPkgMagic = 0x20160905
maxEchoStringLen = 0xff maxEchoStringLen = 0xff
echoHeartbeatRequestString = "ping"
echoHeartbeatResponseString = "pong"
echoMessage = "Hello, getty!"
) )
var ( var (
......
...@@ -29,23 +29,6 @@ type PackageHandler interface { ...@@ -29,23 +29,6 @@ type PackageHandler interface {
} }
//////////////////////////////////////////// ////////////////////////////////////////////
// heartbeat handler
////////////////////////////////////////////
type HeartbeatHandler struct{}
func (this *HeartbeatHandler) Handle(session *getty.Session, pkg *EchoPackage) error {
log.Debug("get echo heartbeat package{%s}", pkg)
var rspPkg EchoPackage
rspPkg.H = pkg.H
rspPkg.B = echoHeartbeatResponseString
rspPkg.H.Len = uint16(len(rspPkg.B))
return session.WritePkg(&rspPkg)
}
////////////////////////////////////////////
// message handler // message handler
//////////////////////////////////////////// ////////////////////////////////////////////
...@@ -74,7 +57,6 @@ type EchoMessageHandler struct { ...@@ -74,7 +57,6 @@ type EchoMessageHandler struct {
func newEchoMessageHandler() *EchoMessageHandler { func newEchoMessageHandler() *EchoMessageHandler {
handlers := make(map[uint32]PackageHandler) handlers := make(map[uint32]PackageHandler)
handlers[heartbeatCmd] = &HeartbeatHandler{}
handlers[echoCmd] = &MessageHandler{} handlers[echoCmd] = &MessageHandler{}
return &EchoMessageHandler{sessionMap: make(map[*getty.Session]*clientEchoSession), handlers: handlers} return &EchoMessageHandler{sessionMap: make(map[*getty.Session]*clientEchoSession), handlers: handlers}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment