Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed encoding part from sender #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/.main.go.swp
Binary file not shown.
18 changes: 17 additions & 1 deletion src/broadcaster.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package main

import (
"fmt"

b64 "encoding/base64"
)

// Broadcaster sends images for broadcast
type Broadcaster struct {
GenericClient
Expand All @@ -11,8 +17,18 @@ func (b *Broadcaster) closeConnection() {
}

func (b *Broadcaster) sendMessage(message []byte) {

if (b.hub.broadcaster == nil) {
fmt.Println("No broadcaster")
return
}

sEnc := b64.StdEncoding.EncodeToString(message)

//fmt.Println(sEnc)

select {
case b.hub.broadcast <- message:
case b.hub.broadcast <- []byte(sEnc):
default:
}
}
12 changes: 8 additions & 4 deletions src/generic_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"log"
"time"

"fmt"
"github.com/gorilla/websocket"
)

Expand All @@ -12,7 +12,7 @@ const (
writeWait = 10 * time.Second

// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second
pongWait = 100 * time.Second

// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
Expand All @@ -39,10 +39,11 @@ func (g GenericClient) Send() chan []byte {

// makeWS is a constructor for generic client
func makeWS(conn *websocket.Conn, hub *Hub) GenericClient {
fmt.Println("CONNEddCT")
return GenericClient{
conn: conn,
hub: hub,
send: make(chan []byte, 1),
send: make(chan []byte, 100),
}
}

Expand All @@ -64,7 +65,7 @@ func readMessages(i SocketInterface) {

for {
_, message, err := i.Conn().ReadMessage()
// fmt.Println(message)
// fmt.Println(message)
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
Expand Down Expand Up @@ -94,6 +95,9 @@ func writeMessages(i SocketInterface) {
return
}

// log.Println(message)
// fmt.Println(message)

w, err := i.Conn().NextWriter(websocket.TextMessage)
if err != nil {
return
Expand Down
13 changes: 9 additions & 4 deletions src/hub.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package main

import (
"fmt"
)

// Hub maintains the set of active clients
type Hub struct {

Expand All @@ -21,9 +25,9 @@ type Hub struct {

func newHub() *Hub {
return &Hub{
register: make(chan *Viewer),
unregister: make(chan *Viewer),
broadcast: make(chan []byte),
register: make(chan *Viewer, 10),
unregister: make(chan *Viewer, 10),
broadcast: make(chan []byte, 100),
clients: make(map[*Viewer]bool),
}
}
Expand All @@ -33,14 +37,15 @@ func (h *Hub) run() {
select {
case client := <-h.register:
h.clients[client] = true
fmt.Println("Connected to the hub!!!")
case client := <-h.unregister:
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
}
case message := <-h.broadcast:
for client := range h.clients {
select {
case client.send <- message:
case client.send <- message: fmt.Println("sent")
default:
client.closeConnection()
}
Expand Down
10 changes: 8 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/http"
"os"
"fmt"

"github.com/gorilla/websocket"
)
Expand Down Expand Up @@ -63,6 +64,8 @@ func main() {
http.Handle("/static/", http.StripPrefix("/static/", fs))

h := newHub()
go h.run()


http.HandleFunc("/viewer", func(w http.ResponseWriter, r *http.Request) {
if h != nil {
Expand All @@ -75,13 +78,16 @@ func main() {

http.HandleFunc("/broadcaster", func(w http.ResponseWriter, r *http.Request) {
if h.broadcaster == nil {
fmt.Println("connecting")

serveBroadcaster(w, r, h)

go h.run()
go readMessages(h.broadcaster)
go writeMessages(h.broadcaster)
}
})

http.ListenAndServe(":3000", nil)
fmt.Println("Server started")

http.ListenAndServe(":8000", nil)
}
3 changes: 3 additions & 0 deletions src/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func (v *Viewer) closeConnection() {
}

func (v *Viewer) sendMessage(message []byte) {
if (v.hub.broadcaster == nil) {
return
}
select {
case v.hub.broadcaster.send <- message:
default:
Expand Down
25 changes: 15 additions & 10 deletions static/camera.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// function b64(e) {
// var t = "";
// var n = new Uint8Array(e);
// var r = n.byteLength;
// for (var i = 0; i < r; i++) {
// t += String.fromCharCode(n[i])
// }
// return window.btoa(t)
// }
function b64(e) {
var t = "";
var n = new Uint8Array(e);
var r = n.byteLength;
for (var i = 0; i < r; i++) {
t += String.fromCharCode(n[i])
}
return window.btoa(t)
}

window.onload = () => {

Expand All @@ -28,6 +28,11 @@ window.onload = () => {
/* change images when a new one is recieved */
let img = document.getElementById('img');
ws.onmessage = (event) => {
img.src = 'data:image/jpg;base64,' + event.data;
// console.log(event)
img.src = 'data:image/jpg;base64,' + (event.data);
}

window.onbeforeunload = () => {
ws.close();
}
}
14 changes: 8 additions & 6 deletions testBroadcaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ async def sendimages(websocket):
while True:
retval, img = cap.read()
retval, buffer = cv2.imencode('.jpg', img)
img = base64.b64encode(buffer)
# img = base64.b64encode(buffer)
#buffer = buffer.tostring()
#print(buffer)
await websocket.send(img)
await asyncio.sleep(0.01)
# await websocket.send(img)
await asyncio.sleep(1.1)

async def recievecontrol(websocket):
async for message in websocket:
while True:
#async for message in websocket:
message = await websocket.recv()
print(message)

async def hello():

async with websockets.connect(
'ws://localhost:3000/broadcaster') as websocket:
'ws://10.0.34.87:8000/broadcaster') as websocket:

send_task = asyncio.ensure_future(sendimages(websocket))
recieve_task = asyncio.ensure_future(recievecontrol(websocket))
Expand All @@ -33,4 +35,4 @@ async def hello():
for task in pending:
task.cancel()

asyncio.get_event_loop().run_until_complete(hello())
asyncio.get_event_loop().run_until_complete(hello())