Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
George Makroglou committed Jul 19, 2024
2 parents fbab944 + bfc27f3 commit 4108da1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
90 changes: 44 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,68 @@


```go
package testruns
package main

import (
"encoding/json"
"fmt"
"encoding/json"
"fmt"

"golang.org/x/net/websocket"
"golang.org/x/net/websocket"
)

// CustomEvents implements the SocketClientEvents interface
type CustomEvents struct {}

func (c CustomEvents) OnConnect(ws *websocket.Conn, sessID string) {
fmt.Println("[CLIENT] Connected with session ID: ", sessID)
fmt.Println("[CLIENT] Connected with session ID: ", sessID)
}

func (c CustomEvents) OnConnectError(err error){
fmt.Println("[CLIENT] Failed to connect: ", err)
fmt.Println("[CLIENT] Failed to connect: ", err)
}

func (c CustomEvents) OnDisconnect(){
fmt.Println("[CLIENT] Disconnected" )
fmt.Println("[CLIENT] Disconnected" )
}

func (c CustomEvents) OnDisconnectError(err error){
fmt.Println("[CLIENT] Failed to disconnect: ", err)
fmt.Println("[CLIENT] Failed to disconnect: ", err)
}

func (c CustomEvents) OnReceive(data map[string]interface{}){
b, _ := json.MarshalIndent(data, "", " ")

b, _ := json.MarshalIndent(data, "", " ")

fmt.Println("[CLIENT] RECEIVED: ", string(b))
fmt.Println("[CLIENT] RECEIVED: ", string(b))
}

func (c CustomEvents) OnReceiveError(err error){
fmt.Println("[CLIENT] Failed to receive: ", err)
fmt.Println("[CLIENT] Failed to receive: ", err)
}

func (c CustomEvents) OnJoin(roomName string){
fmt.Println("[CLIENT] Joined room ", roomName)
fmt.Println("[CLIENT] Joined room ", roomName)
}

func (c CustomEvents) OnJoinError(roomName string, err error){
fmt.Println("[CLIENT] Failed to join room: ", roomName, " ", err)
fmt.Println("[CLIENT] Failed to join room: ", roomName, " ", err)
}

func (c CustomEvents) OnLeave(roomName string){
fmt.Println("[CLIENT] Left room: ", roomName)
fmt.Println("[CLIENT] Left room: ", roomName)
}

func (c CustomEvents) OnLeaveError(roomName string, err error){
fmt.Println("[CLIENT] Failed to leave room: ", roomName, " ", err)
fmt.Println("[CLIENT] Failed to leave room: ", roomName, " ", err)
}

func (c CustomEvents) OnSend(data map[string]interface{}){
b, _ := json.MarshalIndent(data, "", " ")
b, _ := json.MarshalIndent(data, "", " ")

fmt.Println("[CLIENT] SENT: ", string(b))
fmt.Println("[CLIENT] SENT: ", string(b))
}

func (c CustomEvents) OnSendError(err error){
fmt.Println("[CLIENT] Failed to send: ", err)
fmt.Println("[CLIENT] Failed to send: ", err)
}

```
Expand All @@ -85,50 +84,49 @@ func (c CustomEvents) OnSendError(err error){
package main

import (
"fmt"
client "github.com/G-MAKROGLOU/websocket-client"
"sync"
"time"
"fmt"
client "github.com/G-MAKROGLOU/websocket-client"
"sync"
"time"
)

var wg sync.WaitGroup

func main() {
wg.Add(1)
wg.Add(1)

origin := "http://localhost"
server := "ws://localhost:5000/ws"
origin := "http://localhost"
server := "ws://localhost:5000/ws"

client := client.NewSocketClient(origin, server, CustomEvents{})
client := client.NewSocketClient(origin, server, CustomEvents{})

client.Connect()
client.Connect()

client.Join("testRoom")
client.Join("testRoom")

go client.Receive()
go client.Receive()

go testMulticast(client, "CLIENT1")
go testMulticast(client, "CLIENT1")

wg.Wait()
wg.Wait()
}

func testMulticast(client *client.SocketClient, clientName string) {
index := 0
for {
if index == 10 {
fmt.Println("[MULTICAST] DISCONNECTING CLIENT: ", client.ID)
client.Disconnect()
break
}
time.Sleep(5 * time.Second)

index := 0
for {
if index == 10 {
fmt.Println("[MULTICAST] DISCONNECTING CLIENT: ", client.ID)
client.Disconnect()
break
}
time.Sleep(5 * time.Second)

data := map[string]interface{}{
"Message": "[FROM] [" + clientName + "] " + client.ID + " TO ROOM: testRoom",
}
client.SendTo("testRoom", data)
index++
data := map[string]interface{}{
"Message": "[FROM] [" + clientName + "] " + client.ID + " TO ROOM: testRoom",
}
client.SendTo("testRoom", data)
index++
}
}

```
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (sc *SocketClient) Send(data map[string]interface{}) {
sc.events.OnSend(data)
}

// SendTo sends a unitcast/multicast message to all sockets in a room
// SendTo sends a unicast/multicast message to all sockets in a room
func (sc *SocketClient) SendTo(roomName string, data map[string]interface{}) {
data["Gm_Ws_Type"] = "gm_ws_multicast"
data["Gm_Ws_Room"] = roomName
Expand Down

0 comments on commit 4108da1

Please sign in to comment.