File: main.go

package info (click to toggle)
golang-github-francoispqt-gojay 1.2.13-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 86
file content (75 lines) | stat: -rw-r--r-- 2,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// package main simulates a conversation between
// a given set of websocket clients and a server.
//
// It spins up a web socket server.
// On a client's connection it creates a SenderReceiver which handles JSON Stream
// encoding and decoding using gojay's streaming API to abstract JSON communication
// between server and client, only having to handle go values.
//
// To simulate a conversation:
// - the server sends a welcome message to the client
// - when the client receives the message, it sends a message back to the server
// - when the server receives the ack message, it will send a message randomly to a client
// - when the client receives the message, it sends a message back to the server... and so on.
package main

import (
	"log"
	"strconv"

	"github.com/francoispqt/gojay/examples/websocket/client"
	"github.com/francoispqt/gojay/examples/websocket/comm"
	"github.com/francoispqt/gojay/examples/websocket/server"
)

func createServer(done chan error) {
	// create our server, with a done signal
	s := server.NewServer()
	// set our connection handler
	s.OnConnection(func(c *server.Client) {
		// send welcome message to initiate the conversation
		c.SendMessage(&comm.Message{
			UserName: "server",
			Message:  "Welcome !",
		})
		// start handling messages
		c.OnMessage(func(m *comm.Message) {
			log.Print("message received from client: ", m)
			s.BroadCastRandom(c, m)
		})
	})
	go s.Listen(":8070", done)
}

func createClient(url, origin string, i int) {
	// create our client
	c := client.NewClient(i)
	// Dial connection to the WS server
	err := c.Dial(url, origin)
	if err != nil {
		panic(err)
	}
	str := strconv.Itoa(i)
	// Init client's sender and receiver
	// Set the OnMessage handler
	c.OnMessage(func(m *comm.Message) {
		log.Print("client "+str+" received from "+m.UserName+" message: ", m)
		c.SendMessage(&comm.Message{
			UserName: str,
			Message:  "Responding to: " + m.UserName + " | old message: " + m.Message,
		})
	})
}

// Our main function
func main() {
	done := make(chan error)
	createServer(done)
	// add our clients connection
	for i := 0; i < 100; i++ {
		i := i
		go createClient("ws://localhost:8070/ws", "http://localhost/", i)
	}
	// handle server's termination
	log.Fatal(<-done)
}