v2 version release

This commit is contained in:
2021-11-12 16:04:39 +08:00
parent bdcfcd05db
commit 555bc3653e
15 changed files with 3082 additions and 862 deletions
+44 -38
View File
@@ -1,101 +1,107 @@
package starnotify
import (
"errors"
"time"
"b612.me/notify"
"errors"
"sync"
)
var (
starClient map[string]*notify.StarNotifyC
starServer map[string]*notify.StarNotifyS
cmu sync.RWMutex
smu sync.RWMutex
starClient map[string]notify.Client
starServer map[string]notify.Server
)
func init() {
starClient = make(map[string]*notify.StarNotifyC)
starServer = make(map[string]*notify.StarNotifyS)
starClient = make(map[string]notify.Client)
starServer = make(map[string]notify.Server)
}
func NewClient(key, netype, value string) (*notify.StarNotifyC, error) {
client, err := notify.NewNotifyC(netype, value)
if err != nil {
return client, err
}
func NewClient(key string) notify.Client {
client := notify.NewClient()
cmu.Lock()
starClient[key] = client
return client, err
cmu.Unlock()
return client
}
func NewClientWithTimeout(key, netype, value string, timeout time.Duration) (*notify.StarNotifyC, error) {
client, err := notify.NewNotifyCWithTimeOut(netype, value, timeout)
if err != nil {
return client, err
}
starClient[key] = client
return client, err
}
func DeleteClient(key string) error {
func DeleteClient(key string) (err error) {
cmu.RLock()
client, ok := starClient[key]
cmu.RUnlock()
if !ok {
return errors.New("Not Exists Yet!")
}
if client.Online {
client.ClientStop()
if client.Status().Alive {
err = client.Stop()
}
client = nil
cmu.Lock()
delete(starClient, key)
return nil
cmu.Unlock()
return err
}
func NewServer(key, netype, value string) (*notify.StarNotifyS, error) {
server, err := notify.NewNotifyS(netype, value)
if err != nil {
return server, err
}
func NewServer(key string) notify.Server {
server := notify.NewServer()
smu.Lock()
starServer[key] = server
return server, err
smu.Unlock()
return server
}
func DeleteServer(key string) error {
smu.RLock()
server, ok := starServer[key]
smu.RUnlock()
if !ok {
return errors.New("Not Exists Yet!")
}
if server.Online {
server.ServerStop()
if server.Status().Alive {
server.Stop()
}
server = nil
smu.Lock()
delete(starServer, key)
smu.Unlock()
return nil
}
func S(key string) *notify.StarNotifyS {
func S(key string) notify.Server {
smu.RLock()
server, ok := starServer[key]
smu.RUnlock()
if !ok {
return nil
}
return server
}
func C(key string) *notify.StarNotifyC {
func C(key string) notify.Client {
cmu.RLock()
client, ok := starClient[key]
cmu.RUnlock()
if !ok {
return nil
}
return client
}
func Server(key string) (*notify.StarNotifyS, error) {
func Server(key string) (notify.Server, error) {
smu.RLock()
server, ok := starServer[key]
smu.RUnlock()
if !ok {
return nil, errors.New("Not Exists Yet")
}
return server, nil
}
func Client(key string) (*notify.StarNotifyC, error) {
func Client(key string) (notify.Client, error) {
cmu.RLock()
client, ok := starClient[key]
cmu.RUnlock()
if !ok {
return nil, errors.New("Not Exists Yet")
}