Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
galene
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
galene
Commits
4ea4e7c0
Commit
4ea4e7c0
authored
Apr 25, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement chat history.
parent
1d90f443
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
1 deletion
+55
-1
client.go
client.go
+16
-1
group.go
group.go
+32
-0
static/sfu.js
static/sfu.js
+7
-0
No files found.
client.go
View file @
4ea4e7c0
...
@@ -127,7 +127,7 @@ func startClient(conn *websocket.Conn) (err error) {
...
@@ -127,7 +127,7 @@ func startClient(conn *websocket.Conn) (err error) {
defer
close
(
c
.
done
)
defer
close
(
c
.
done
)
c
.
writeCh
=
make
(
chan
interface
{},
1
)
c
.
writeCh
=
make
(
chan
interface
{},
25
)
defer
func
()
{
defer
func
()
{
if
isWSNormalError
(
err
)
{
if
isWSNormalError
(
err
)
{
err
=
nil
err
=
nil
...
@@ -693,6 +693,20 @@ func clientLoop(c *client, conn *websocket.Conn) error {
...
@@ -693,6 +693,20 @@ func clientLoop(c *client, conn *websocket.Conn) error {
cc
.
action
(
pushTracksAction
{
c
})
cc
.
action
(
pushTracksAction
{
c
})
}
}
h
:=
c
.
group
.
getChatHistory
()
for
_
,
m
:=
range
h
{
err
:=
c
.
write
(
clientMessage
{
Type
:
"chat"
,
Id
:
m
.
id
,
Username
:
m
.
user
,
Value
:
m
.
value
,
Me
:
m
.
me
,
})
if
err
!=
nil
{
return
err
}
}
ticker
:=
time
.
NewTicker
(
2
*
time
.
Second
)
ticker
:=
time
.
NewTicker
(
2
*
time
.
Second
)
defer
ticker
.
Stop
()
defer
ticker
.
Stop
()
...
@@ -819,6 +833,7 @@ func handleClientMessage(c *client, m clientMessage) error {
...
@@ -819,6 +833,7 @@ func handleClientMessage(c *client, m clientMessage) error {
log
.
Printf
(
"ICE: %v"
,
err
)
log
.
Printf
(
"ICE: %v"
,
err
)
}
}
case
"chat"
:
case
"chat"
:
c
.
group
.
addToChatHistory
(
m
.
Id
,
m
.
Username
,
m
.
Value
,
m
.
Me
)
clients
:=
c
.
group
.
getClients
(
c
)
clients
:=
c
.
group
.
getClients
(
c
)
for
_
,
cc
:=
range
clients
{
for
_
,
cc
:=
range
clients
{
cc
.
write
(
m
)
cc
.
write
(
m
)
...
...
group.go
View file @
4ea4e7c0
...
@@ -51,6 +51,13 @@ type client struct {
...
@@ -51,6 +51,13 @@ type client struct {
up
map
[
string
]
*
upConnection
up
map
[
string
]
*
upConnection
}
}
type
chatHistoryEntry
struct
{
id
string
user
string
value
string
me
bool
}
type
group
struct
{
type
group
struct
{
name
string
name
string
dead
bool
dead
bool
...
@@ -58,6 +65,7 @@ type group struct {
...
@@ -58,6 +65,7 @@ type group struct {
mu
sync
.
Mutex
mu
sync
.
Mutex
clients
[]
*
client
clients
[]
*
client
history
[]
chatHistoryEntry
}
}
type
delPCAction
struct
{
type
delPCAction
struct
{
...
@@ -259,6 +267,30 @@ func (g *group) Range(f func(c *client) bool) {
...
@@ -259,6 +267,30 @@ func (g *group) Range(f func(c *client) bool) {
}
}
}
}
const
maxChatHistory
=
20
func
(
g
*
group
)
addToChatHistory
(
id
,
user
,
value
string
,
me
bool
)
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
if
len
(
g
.
history
)
>=
maxChatHistory
{
copy
(
g
.
history
,
g
.
history
[
1
:
])
g
.
history
=
g
.
history
[
:
len
(
g
.
history
)
-
1
]
}
g
.
history
=
append
(
g
.
history
,
chatHistoryEntry
{
id
:
id
,
user
:
user
,
value
:
value
,
me
:
me
},
)
}
func
(
g
*
group
)
getChatHistory
()
[]
chatHistoryEntry
{
g
.
mu
.
Lock
()
g
.
mu
.
Unlock
()
h
:=
make
([]
chatHistoryEntry
,
len
(
g
.
history
))
copy
(
h
,
g
.
history
)
return
h
}
type
writerDeadError
int
type
writerDeadError
int
func
(
err
writerDeadError
)
Error
()
string
{
func
(
err
writerDeadError
)
Error
()
string
{
...
...
static/sfu.js
View file @
4ea4e7c0
...
@@ -283,6 +283,7 @@ function serverConnect() {
...
@@ -283,6 +283,7 @@ function serverConnect() {
};
};
socket
.
onopen
=
function
(
e
)
{
socket
.
onopen
=
function
(
e
)
{
resetUsers
();
resetUsers
();
resetChat
();
setConnected
(
true
);
setConnected
(
true
);
let
up
=
getUserPass
();
let
up
=
getUserPass
();
send
({
send
({
...
@@ -648,6 +649,11 @@ function addToChatbox(peerId, nick, message, me){
...
@@ -648,6 +649,11 @@ function addToChatbox(peerId, nick, message, me){
return
message
;
return
message
;
}
}
function
resetChat
()
{
lastMessage
=
{};
document
.
getElementById
(
'
box
'
).
textContent
=
''
;
}
function
handleInput
()
{
function
handleInput
()
{
let
username
=
getUsername
();
let
username
=
getUsername
();
let
input
=
document
.
getElementById
(
'
input
'
);
let
input
=
document
.
getElementById
(
'
input
'
);
...
@@ -729,6 +735,7 @@ function handleInput() {
...
@@ -729,6 +735,7 @@ function handleInput() {
addToChatbox
(
myid
,
username
,
message
,
me
);
addToChatbox
(
myid
,
username
,
message
,
me
);
send
({
send
({
type
:
'
chat
'
,
type
:
'
chat
'
,
id
:
myid
,
username
:
username
,
username
:
username
,
value
:
message
,
value
:
message
,
me
:
me
,
me
:
me
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment