|
|
@ -9,11 +9,14 @@ import (
|
|
|
|
var connc net.Conn
|
|
|
|
var connc net.Conn
|
|
|
|
|
|
|
|
|
|
|
|
var clientSign map[string]chan string
|
|
|
|
var clientSign map[string]chan string
|
|
|
|
|
|
|
|
var clientStopSign chan int
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
clientStopSign = make(chan int)
|
|
|
|
clientSign = make(map[string]chan string)
|
|
|
|
clientSign = make(map[string]chan string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Notify 用于获取一个通知
|
|
|
|
func Notify(key string) chan string {
|
|
|
|
func Notify(key string) chan string {
|
|
|
|
if _, ok := clientSign[key]; !ok {
|
|
|
|
if _, ok := clientSign[key]; !ok {
|
|
|
|
ch := make(chan string, 5)
|
|
|
|
ch := make(chan string, 5)
|
|
|
@ -32,6 +35,7 @@ func store(key, value string) {
|
|
|
|
clientSign[key] <- value
|
|
|
|
clientSign[key] <- value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// NewNotifyC 用于新建一个Client端进程
|
|
|
|
func NewNotifyC(netype, value string) error {
|
|
|
|
func NewNotifyC(netype, value string) error {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
connc, err = net.Dial(netype, value)
|
|
|
|
connc, err = net.Dial(netype, value)
|
|
|
@ -41,6 +45,12 @@ func NewNotifyC(netype, value string) error {
|
|
|
|
go cnotify()
|
|
|
|
go cnotify()
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-clientStopSign:
|
|
|
|
|
|
|
|
connc.Close()
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
|
|
|
buf := make([]byte, 8192)
|
|
|
|
buf := make([]byte, 8192)
|
|
|
|
n, err := connc.Read(buf)
|
|
|
|
n, err := connc.Read(buf)
|
|
|
|
Queue.ParseMessage(buf[0:n], connc)
|
|
|
|
Queue.ParseMessage(buf[0:n], connc)
|
|
|
@ -55,6 +65,7 @@ func NewNotifyC(netype, value string) error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send 用于向Server端发送数据
|
|
|
|
func Send(name string) error {
|
|
|
|
func Send(name string) error {
|
|
|
|
_, err := connc.Write(Queue.BuildMessage(name))
|
|
|
|
_, err := connc.Write(Queue.BuildMessage(name))
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -63,6 +74,8 @@ func Send(name string) error {
|
|
|
|
func cnotify() {
|
|
|
|
func cnotify() {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-clientStopSign:
|
|
|
|
|
|
|
|
break
|
|
|
|
case <-notifychan:
|
|
|
|
case <-notifychan:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
default:
|
|
|
@ -79,3 +92,8 @@ func cnotify() {
|
|
|
|
go store(strs[0], strs[1])
|
|
|
|
go store(strs[0], strs[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ClientStop 终止client端运行
|
|
|
|
|
|
|
|
func ClientStop() {
|
|
|
|
|
|
|
|
clientStopSign <- 0
|
|
|
|
|
|
|
|
}
|
|
|
|