Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tockn committed Apr 29, 2020
1 parent 98e67a3 commit 93c7ef6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 119 deletions.
16 changes: 8 additions & 8 deletions handler/create_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/tockn/singo/model"
)

type ResponseCreateConnection struct {
type SendClientID struct {
ClientID string `json:"client_id"`
}

Expand All @@ -23,11 +23,11 @@ func (h *Handler) CreateConnection(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
go h.HandleSendMessage(ctx, c, conn)
go h.HandleReceiveMessage(c, conn)
resp := &ResponseCreateConnection{ClientID: c.ID}
body, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(body)
resp := &SendClientID{ClientID: c.ID}
payload, _ := json.Marshal(resp)
body, _ := json.Marshal(SendMessage{
Type: SendMessageTypeNotifyClientID,
Payload: payload,
})
sendMessage(conn, body)
}
15 changes: 11 additions & 4 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package handler

import (
"encoding/json"
"log"
"net/http"

"github.com/tockn/singo/manager"

Expand All @@ -19,12 +21,15 @@ func NewHandler(man *manager.Manager) *Handler {
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}

type ReceiveMessageType string

var (
ReceiveMessageTypeJoinRoom ReceiveMessageType = "join room"
ReceiveMessageTypeJoinRoom ReceiveMessageType = "join"
ReceiveMessageTypeOffer ReceiveMessageType = "offer"
ReceiveMessageTypeAnswer ReceiveMessageType = "answer"
)
Expand All @@ -37,9 +42,10 @@ type ReceiveMessage struct {
type SendMessageType string

var (
SendMessageTypeError SendMessageType = "error"
SendMessageTypeOffer SendMessageType = "offer"
SendMessageTypeAnswer SendMessageType = "answer"
SendMessageTypeNotifyClientID SendMessageType = "notify-client-id"
SendMessageTypeError SendMessageType = "error"
SendMessageTypeOffer SendMessageType = "offer"
SendMessageTypeAnswer SendMessageType = "answer"
)

type ErrorMessage string
Expand All @@ -60,6 +66,7 @@ type SendMessage struct {
}

func newErrorMessage(msg error) *SendMessage {
log.Println("error:", msg)
return &SendMessage{
Type: SendMessageTypeError,
Payload: []byte(msg.Error()),
Expand Down
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log"
"net/http"

"github.com/go-chi/chi"
"github.com/tockn/singo/handler"
"github.com/tockn/singo/manager"
"github.com/tockn/singo/repository/mem"
Expand All @@ -21,9 +20,8 @@ func run() error {
man := manager.NewManager(roomRepo)
han := handler.NewHandler(man)

r := chi.NewRouter()
r.Get("/connect", han.CreateConnection)
http.HandleFunc("/connect", han.CreateConnection)

log.Println("running...")
return http.ListenAndServe(":5000", r)
return http.ListenAndServe("0.0.0.0:5000", nil)
}
11 changes: 8 additions & 3 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ func (m *Manager) CreateRoom(name string) (*model.Room, error) {

func (m *Manager) JoinRoom(c *model.Client, roomID string) error {
r, err := m.roomRepo.Get(roomID)
if err != nil {
if err == repository.ErrNotFound {
r = model.NewRoom(roomID)
if _, err := m.roomRepo.Create(r); err != nil {
return err
}
} else if err != nil {
return err
}
r.Clients[c.ID] = c
Expand All @@ -41,7 +46,7 @@ func (m *Manager) TransferSDPOffer(senderClient *model.Client, sdp *model.SDP) e
}
msg := &model.Message{
Type: model.MessageTypeSDPOffer,
Payload: &SDPOfferPayload{SDP: sdp},
Payload: SDPOfferPayload{SDP: sdp},
}
for _, c := range r.Clients {
if c.ID == senderClient.ID {
Expand All @@ -63,7 +68,7 @@ func (m *Manager) TransferSDPAnswer(senderClient *model.Client, sdp *model.SDP)
}
msg := &model.Message{
Type: model.MessageTypeSDPAnswer,
Payload: &SDPAnswerPayload{SDP: sdp},
Payload: SDPAnswerPayload{SDP: sdp},
}
for _, c := range r.Clients {
if c.ID == senderClient.ID {
Expand Down
11 changes: 11 additions & 0 deletions repository/mem/room.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mem

import (
"sync"

"github.com/tockn/singo/model"
"github.com/tockn/singo/repository"
)
Expand All @@ -12,10 +14,13 @@ func NewRoomRepository() repository.Room {
}

type roomRepository struct {
mutex sync.Mutex
rooms map[string]*model.Room
}

func (re *roomRepository) Get(roomID string) (*model.Room, error) {
re.mutex.Lock()
defer re.mutex.Unlock()
r, ok := re.rooms[roomID]
if !ok {
return nil, repository.ErrNotFound
Expand All @@ -24,6 +29,8 @@ func (re *roomRepository) Get(roomID string) (*model.Room, error) {
}

func (re *roomRepository) Update(r *model.Room) (*model.Room, error) {
re.mutex.Lock()
defer re.mutex.Unlock()
if _, ok := re.rooms[r.ID]; !ok {
return nil, repository.ErrNotFound
}
Expand All @@ -32,11 +39,15 @@ func (re *roomRepository) Update(r *model.Room) (*model.Room, error) {
}

func (re *roomRepository) Create(r *model.Room) (*model.Room, error) {
re.mutex.Lock()
defer re.mutex.Unlock()
re.rooms[r.ID] = r
return r, nil
}

func (re *roomRepository) GetByClientID(clientID string) (*model.Room, error) {
re.mutex.Lock()
defer re.mutex.Unlock()
for _, r := range re.rooms {
for _, c := range r.Clients {
if c.ID != clientID {
Expand Down
100 changes: 0 additions & 100 deletions sdk/dist/bundle.js

This file was deleted.

0 comments on commit 93c7ef6

Please sign in to comment.