Commit 9de187bb authored by alexstocks's avatar alexstocks

add ws client

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