Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
9de187bb
Commit
9de187bb
authored
Oct 14, 2016
by
alexstocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add ws client
parent
70d1d8bc
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
22 additions
and
71 deletions
+22
-71
client.go
echo/ws-echo/client/app/client.go
+2
-22
config.go
echo/ws-echo/client/app/config.go
+1
-0
echo.go
echo/ws-echo/client/app/echo.go
+5
-10
handler.go
echo/ws-echo/client/app/handler.go
+2
-5
main.go
echo/ws-echo/client/app/main.go
+2
-2
config.toml
echo/ws-echo/client/profiles/test/config.toml
+2
-1
log.xml
echo/ws-echo/client/profiles/test/log.xml
+1
-1
main.js
echo/ws-echo/js-client/main.js
+5
-4
.handler.go.swp
echo/ws-echo/server/app/.handler.go.swp
+0
-0
echo.go
echo/ws-echo/server/app/echo.go
+2
-8
handler.go
echo/ws-echo/server/app/handler.go
+0
-18
No files found.
echo/ws-echo/client/app/client.go
View file @
9de187bb
...
@@ -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
)
}
}
echo/ws-echo/client/app/config.go
View file @
9de187bb
...
@@ -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
...
...
echo/ws-echo/client/app/
utils
.go
→
echo/ws-echo/client/app/
echo
.go
View file @
9de187bb
/******************************************************
/******************************************************
# 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
}
}
echo/ws-echo/client/app/handler.go
View file @
9de187bb
...
@@ -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
)
}
}
echo/ws-echo/client/app/main.go
View file @
9de187bb
...
@@ -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
)
...
...
echo/ws-echo/client/profiles/test/config.toml
View file @
9de187bb
...
@@ -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
...
...
echo/ws-echo/client/profiles/test/log.xml
View file @
9de187bb
<logging>
<logging>
<filter
enabled=
"
fals
e"
>
<filter
enabled=
"
tru
e"
>
<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) -->
...
...
echo/ws-echo/js-client/main.js
View file @
9de187bb
...
@@ -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'
});
});
}
}
...
...
echo/ws-echo/server/app/.handler.go.swp
0 → 100644
View file @
9de187bb
File added
echo/ws-echo/server/app/echo.go
View file @
9de187bb
...
@@ -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
(
...
...
echo/ws-echo/server/app/handler.go
View file @
9de187bb
...
@@ -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
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment