Skip to content

Commit

Permalink
initial : sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef-Shehata committed Nov 8, 2024
1 parent 2808201 commit 58b88a3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/server.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[2024-11-08 20:07:57] ERROR : server listening
[2024-11-08 20:10:06] ERROR : server listening
[2024-11-08 20:11:07] ERROR : server listening
[2024-11-08 20:35:48] ERROR : strating server
[2024-11-08 20:35:48] INFO : SERVER LISTENING ON PORT : 8080
[2024-11-08 20:49:38] INFO : SIGNING YOU UPPP
[2024-11-08 20:49:39] ERROR : Creating User: table Users has no column named updated_at
[2024-11-08 20:51:49] INFO : SIGNING YOU UPPP
[2024-11-08 20:51:50] ERROR : Creating User: no such function: gen_random_uuid
[2024-11-08 21:10:54] INFO : SIGNING YOU UPPP
55 changes: 55 additions & 0 deletions pkg/socket_hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import "github.com/gorilla/websocket"
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type Client struct {
hub *Hub
// The websocket connection.
conn *websocket.Conn
// Buffered channel of outbound messages.
send chan []byte
}

type Hub struct {
clients map[*Client]bool
register chan *Client
unregister chan *Client
broadcast chan []byte
}

func newHub() *Hub {
return &Hub{
broadcast: make(chan []byte),
clients: make(map[*Client]bool),
register: make(chan *Client),
unregister: make(chan *Client),
}
}

func (h *Hub) run() {

for {
select {
case client := <-h.register:
h.clients[client] = true

case client := <-h.unregister:
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
close(client.send)
}
case message := <-h.broadcast:
for client := range h.clients {
select {
case client.send <- message:
default:
close(client.send)
delete(h.clients, client)
}
}
}
}
}

0 comments on commit 58b88a3

Please sign in to comment.