/
chatroom.go
115 lines (101 loc) · 3.03 KB
/
chatroom.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2013 Beego Samples authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package controllers
import (
"container/list"
"time"
"github.com/astaxie/beego"
"github.com/gorilla/websocket"
"github.com/beego/samples/WebIM/models"
)
type Subscription struct {
Archive []models.Event // All the events from the archive.
New <-chan models.Event // New events coming in.
}
func newEvent(ep models.EventType, user, msg string) models.Event {
return models.Event{ep, user, int(time.Now().Unix()), msg}
}
func Join(user string, ws *websocket.Conn) {
subscribe <- Subscriber{Name: user, Conn: ws}
}
func Leave(user string) {
unsubscribe <- user
}
type Subscriber struct {
Name string
Conn *websocket.Conn // Only for WebSocket users; otherwise nil.
}
var (
// Channel for new join users.
subscribe = make(chan Subscriber, 10)
// Channel for exit users.
unsubscribe = make(chan string, 10)
// Send events here to publish them.
publish = make(chan models.Event, 10)
// Long polling waiting list.
waitingList = list.New()
subscribers = list.New()
)
// This function handles all incoming chan messages.
func chatroom() {
for {
select {
case sub := <-subscribe:
if !isUserExist(subscribers, sub.Name) {
subscribers.PushBack(sub) // Add user to the end of list.
// Publish a JOIN event.
publish <- newEvent(models.EVENT_JOIN, sub.Name, "")
beego.Info("New user:", sub.Name, ";WebSocket:", sub.Conn != nil)
} else {
beego.Info("Old user:", sub.Name, ";WebSocket:", sub.Conn != nil)
}
case event := <-publish:
// Notify waiting list.
for ch := waitingList.Back(); ch != nil; ch = ch.Prev() {
ch.Value.(chan bool) <- true
waitingList.Remove(ch)
}
broadcastWebSocket(event)
models.NewArchive(event)
if event.Type == models.EVENT_MESSAGE {
beego.Info("Message from", event.User, ";Content:", event.Content)
}
case unsub := <-unsubscribe:
for sub := subscribers.Front(); sub != nil; sub = sub.Next() {
if sub.Value.(Subscriber).Name == unsub {
subscribers.Remove(sub)
// Clone connection.
ws := sub.Value.(Subscriber).Conn
if ws != nil {
ws.Close()
beego.Error("WebSocket closed:", unsub)
}
publish <- newEvent(models.EVENT_LEAVE, unsub, "") // Publish a LEAVE event.
break
}
}
}
}
}
func init() {
go chatroom()
}
func isUserExist(subscribers *list.List, user string) bool {
for sub := subscribers.Front(); sub != nil; sub = sub.Next() {
if sub.Value.(Subscriber).Name == user {
return true
}
}
return false
}