Skip to content

Commit

Permalink
Merge pull request #10 from dewep-online/develop
Browse files Browse the repository at this point in the history
upgrade websocket plugin
  • Loading branch information
markus621 authored Jul 30, 2022
2 parents b573b3e + 2a3bac9 commit dce8164
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 157 deletions.
18 changes: 13 additions & 5 deletions examples/demo-ws/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
$('#tick_count').html(tick);
}

function approve() {
socket.send($('#data').val())
function ev(id) {
socket.send($('#data'+id).val())
}


Expand All @@ -46,9 +46,17 @@
<a href="#" onclick="socket.close('1000')">Close socket</a>
<br>
<br>
<input type="text" id="data" style="width: 400px"
value='{"id":1,"d":[1,2,3,4,5]}'><input
type="button" onclick="approve()" value="Send">
<input type="text" id="data1" style="width: 400px"
value='{"e":1,"d":[1,2,3,4,5]}'><input
type="button" onclick="ev(1)" value="Send">
<br>
<input type="text" id="data2" style="width: 400px"
value='{"e":11}'><input
type="button" onclick="ev(2)" value="Send">
<br>
<input type="text" id="data3" style="width: 400px"
value='{"e":13}'><input
type="button" onclick="ev(3)" value="Send">
<br>
<br>

Expand Down
62 changes: 52 additions & 10 deletions examples/demo-ws/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import (
"fmt"
"sync"
"time"

"github.com/dewep-online/goppy"
"github.com/dewep-online/goppy/middlewares"
"github.com/dewep-online/goppy/plugins"
Expand All @@ -21,8 +25,8 @@ func main() {
router := routes.Main()
router.Use(middlewares.ThrottlingMiddleware(100))

ws.Event(1, c.List)
ws.Event(2, c.User)
ws.Event(c.OneEvent, 1, 2)
ws.Event(c.MultiEvent, 11, 13)

router.Get("/ws", ws.Handling)
},
Expand All @@ -31,22 +35,60 @@ func main() {
app.Run()
}

type Controller struct{}
type Controller struct {
list map[string]http.Processor
mux sync.RWMutex
}

func NewController() *Controller {
return &Controller{}
c := &Controller{
list: make(map[string]http.Processor),
}
go c.Timer()
return c
}

func (v *Controller) List(m http.Message, c http.Connection) error {
func (v *Controller) OneEvent(d http.Eventer, c http.Processor) error {
list := make([]int, 0)
if err := m.Decode(&list); err != nil {
if err := d.Decode(&list); err != nil {
return err
}
list = append(list, 10, 19, 17, 15)
return m.Encode(&list)
c.Encode(1, &list)
return nil
}

func (v *Controller) User(m http.Message, c http.Connection) error {
id := c.UID()
return m.Encode(&id)
func (v *Controller) Timer() {
t := time.NewTicker(time.Second * 3)
defer t.Stop()

for {
select {
case tt := <-t.C:
v.mux.RLock()
for _, p := range v.list {
p.Encode(12, tt.Format(time.RFC3339))
}
v.mux.RUnlock()
}
}
}

func (v *Controller) MultiEvent(d http.Eventer, c http.Processor) error {
v.mux.Lock()
defer v.mux.Unlock()

switch d.EventID() {
case 11:
v.list[c.CID()] = c
fmt.Println("add", c.CID())
c.OnClose(func(cid string) {
fmt.Println("del", cid)
delete(v.list, cid)
})
case 13:
fmt.Println("del", c.CID())
delete(v.list, c.CID())
}
return nil
}
Loading

0 comments on commit dce8164

Please sign in to comment.