Timestamp chat messages.
This commit is contained in:
+29
-2
@@ -142,6 +142,7 @@ type clientMessage struct {
|
||||
Permissions group.ClientPermissions `json:"permissions,omitempty"`
|
||||
Group string `json:"group,omitempty"`
|
||||
Value string `json:"value,omitempty"`
|
||||
Time uint64 `json:"time,omitempty"`
|
||||
Offer *webrtc.SessionDescription `json:"offer,omitempty"`
|
||||
Answer *webrtc.SessionDescription `json:"answer,omitempty"`
|
||||
Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"`
|
||||
@@ -149,6 +150,20 @@ type clientMessage struct {
|
||||
Request rateMap `json:"request,omitempty"`
|
||||
}
|
||||
|
||||
func fromJSTime(tm uint64) time.Time {
|
||||
if tm == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
return time.Unix(int64(tm)/1000, (int64(tm)%1000) * 1000000)
|
||||
}
|
||||
|
||||
func toJSTime(tm time.Time) uint64 {
|
||||
if tm.Before(time.Unix(0, 0)) {
|
||||
return 0
|
||||
}
|
||||
return uint64((tm.Sub(time.Unix(0, 0)) + time.Millisecond / 2) / time.Millisecond)
|
||||
}
|
||||
|
||||
type closeMessage struct {
|
||||
data []byte
|
||||
}
|
||||
@@ -714,6 +729,7 @@ func clientLoop(c *webClient, ws *websocket.Conn) error {
|
||||
Type: "chat",
|
||||
Id: m.Id,
|
||||
Username: m.User,
|
||||
Time: m.Time,
|
||||
Value: m.Value,
|
||||
Kind: m.Kind,
|
||||
})
|
||||
@@ -984,12 +1000,23 @@ func handleClientMessage(c *webClient, m clientMessage) error {
|
||||
log.Printf("ICE: %v", err)
|
||||
}
|
||||
case "chat":
|
||||
c.group.AddToChatHistory(m.Id, m.Username, m.Kind, m.Value)
|
||||
tm := toJSTime(time.Now())
|
||||
c.group.AddToChatHistory(
|
||||
m.Id, m.Username, tm, m.Kind, m.Value,
|
||||
)
|
||||
mm := clientMessage{
|
||||
Type: "chat",
|
||||
Id: m.Id,
|
||||
Username: m.Username,
|
||||
Time: tm,
|
||||
Kind: m.Kind,
|
||||
Value: m.Value,
|
||||
}
|
||||
clients := c.group.GetClients(nil)
|
||||
for _, cc := range clients {
|
||||
cc, ok := cc.(*webClient)
|
||||
if ok {
|
||||
cc.write(m)
|
||||
cc.write(mm)
|
||||
}
|
||||
}
|
||||
case "groupaction":
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package rtpconn
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestJSTime(t *testing.T) {
|
||||
tm := time.Now()
|
||||
js := toJSTime(tm)
|
||||
tm2 := fromJSTime(js)
|
||||
js2 := toJSTime(tm2)
|
||||
|
||||
if js != js2 {
|
||||
t.Errorf("%v != %v", js, js2)
|
||||
}
|
||||
|
||||
delta := tm.Sub(tm2)
|
||||
if delta < -time.Millisecond/2 || delta > time.Millisecond/2 {
|
||||
t.Errorf("Delta %v, %v, %v", delta, tm, tm2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user