parent
fcb966dda6
commit
5516313c79
|
@ -11,6 +11,10 @@ import (
|
||||||
"github.com/zorchenhimer/MovieNight/common"
|
"github.com/zorchenhimer/MovieNight/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func connSend(s string, c *websocket.Conn) {
|
||||||
|
c.WriteMessage(websocket.TextMessage, []byte(s))
|
||||||
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
name string // Display name
|
name string // Display name
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
|
@ -108,8 +112,8 @@ func (cl *Client) Exit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Sending message block to the client
|
//Sending message block to the client
|
||||||
func (cl *Client) Send(msgs string) {
|
func (cl *Client) Send(s string) {
|
||||||
cl.conn.WriteMessage(websocket.TextMessage, []byte(msgs))
|
connSend(s, cl.conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send server message to this client
|
// Send server message to this client
|
||||||
|
|
46
chatroom.go
46
chatroom.go
|
@ -1,8 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
uuid "github.com/satori/go.uuid"
|
||||||
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -24,19 +27,23 @@ var re_username *regexp.Regexp = regexp.MustCompile(`^[0-9a-zA-Z_-]+$`)
|
||||||
type ChatRoom struct {
|
type ChatRoom struct {
|
||||||
clients map[string]*Client // this needs to be a pointer.
|
clients map[string]*Client // this needs to be a pointer.
|
||||||
clientsMtx sync.Mutex
|
clientsMtx sync.Mutex
|
||||||
|
tempConn map[string]*websocket.Conn
|
||||||
queue chan string
|
queue chan string
|
||||||
playing string
|
playing string
|
||||||
playingLink string
|
playingLink string
|
||||||
}
|
}
|
||||||
|
|
||||||
//initializing the chatroom
|
//initializing the chatroom
|
||||||
func (cr *ChatRoom) Init() error {
|
func newChatRoom() (*ChatRoom, error) {
|
||||||
cr.queue = make(chan string, 5)
|
cr := &ChatRoom{
|
||||||
cr.clients = make(map[string]*Client)
|
queue: make(chan string, 5),
|
||||||
|
clients: make(map[string]*Client),
|
||||||
|
tempConn: make(map[string]*websocket.Conn),
|
||||||
|
}
|
||||||
|
|
||||||
num, err := LoadEmotes()
|
num, err := LoadEmotes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error loading emotes: %s", err)
|
return nil, fmt.Errorf("Error loading emotes: %s", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Loaded %d emotes\n", num)
|
fmt.Printf("Loaded %d emotes\n", num)
|
||||||
|
|
||||||
|
@ -47,7 +54,7 @@ func (cr *ChatRoom) Init() error {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return nil
|
return cr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadEmotes() (int, error) {
|
func LoadEmotes() (int, error) {
|
||||||
|
@ -87,9 +94,32 @@ func randomColor() string {
|
||||||
nums[3], nums[4], nums[5])
|
nums[3], nums[4], nums[5])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cr *ChatRoom) JoinTemp(conn *websocket.Conn) (string, error) {
|
||||||
|
if conn == nil {
|
||||||
|
return "", errors.New("conn should not be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
uid, err := uuid.NewV4()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("could not create uuid, %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
suid := uid.String()
|
||||||
|
if _, ok := cr.tempConn[suid]; ok {
|
||||||
|
return "", errors.New("%#v is already in the temp connections")
|
||||||
|
}
|
||||||
|
|
||||||
|
cr.tempConn[suid] = conn
|
||||||
|
return suid, nil
|
||||||
|
}
|
||||||
|
|
||||||
//registering a new client
|
//registering a new client
|
||||||
//returns pointer to a Client, or Nil, if the name is already taken
|
//returns pointer to a Client, or Nil, if the name is already taken
|
||||||
func (cr *ChatRoom) Join(name string, conn *websocket.Conn) (*Client, error) {
|
func (cr *ChatRoom) Join(name, uid string) (*Client, error) {
|
||||||
|
conn, hasConn := cr.tempConn[uid]
|
||||||
|
if !hasConn {
|
||||||
|
return nil, errors.New("connection is missing from temp connections")
|
||||||
|
}
|
||||||
|
|
||||||
if len(name) < UsernameMinLength || len(name) > UsernameMaxLength || !re_username.MatchString(name) {
|
if len(name) < UsernameMinLength || len(name) > UsernameMaxLength || !re_username.MatchString(name) {
|
||||||
return nil, UserFormatError{Name: name}
|
return nil, UserFormatError{Name: name}
|
||||||
|
@ -115,6 +145,7 @@ func (cr *ChatRoom) Join(name string, conn *websocket.Conn) (*Client, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cr.clients[strings.ToLower(name)] = client
|
cr.clients[strings.ToLower(name)] = client
|
||||||
|
delete(cr.tempConn, uid)
|
||||||
|
|
||||||
fmt.Printf("[join] %s %s\n", host, name)
|
fmt.Printf("[join] %s %s\n", host, name)
|
||||||
//client.Send(cr.GetPlayingString())
|
//client.Send(cr.GetPlayingString())
|
||||||
|
@ -329,6 +360,9 @@ infLoop:
|
||||||
for _, client := range cr.clients {
|
for _, client := range cr.clients {
|
||||||
client.Send(msgBlock)
|
client.Send(msgBlock)
|
||||||
}
|
}
|
||||||
|
for _, conn := range cr.tempConn {
|
||||||
|
connSend(msgBlock, conn)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
handlers.go
14
handlers.go
|
@ -18,7 +18,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
//global variable for handling all chat traffic
|
//global variable for handling all chat traffic
|
||||||
chat ChatRoom
|
chat *ChatRoom
|
||||||
|
|
||||||
// Read/Write mutex for rtmp stream
|
// Read/Write mutex for rtmp stream
|
||||||
l = &sync.RWMutex{}
|
l = &sync.RWMutex{}
|
||||||
|
@ -97,6 +97,12 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
go func() {
|
go func() {
|
||||||
var client *Client
|
var client *Client
|
||||||
|
|
||||||
|
uid, err := chat.JoinTemp(conn)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("[handler] could not do a temp join, %v\n", err)
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
//first message has to be the name
|
//first message has to be the name
|
||||||
// loop through name since websocket is opened once
|
// loop through name since websocket is opened once
|
||||||
for client == nil {
|
for client == nil {
|
||||||
|
@ -108,7 +114,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
name := string(msg)
|
name := string(msg)
|
||||||
client, err = chat.Join(name, conn)
|
client, err = chat.Join(name, uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err.(type) {
|
switch err.(type) {
|
||||||
case UserFormatError, UserTakenError:
|
case UserFormatError, UserTakenError:
|
||||||
|
@ -117,6 +123,10 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Printf("[BAN] %v\n", err)
|
fmt.Printf("[BAN] %v\n", err)
|
||||||
// close connection since banned users shouldn't be connecting
|
// close connection since banned users shouldn't be connecting
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
default:
|
||||||
|
// for now all errors not caught need to be warned
|
||||||
|
fmt.Printf("[handler] %v\n", err)
|
||||||
|
conn.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue