Browse Source

Add current ping indicator

wip
Trevor Slocum 3 years ago
parent
commit
2443590cde
  1. 13
      pkg/web/message.go
  2. 66
      pkg/web/public/assets/js/harmony.js
  3. 2
      pkg/web/web.go

13
pkg/web/message.go

@ -1,12 +1,15 @@
package web
import "fmt"
type MessageType int
const (
MessageBinary MessageType = 2
MessagePing MessageType = 100
MessageCall MessageType = 101
MessageAnswer MessageType = 102
MessagePing MessageType = 101
MessagePong MessageType = 102
MessageCall MessageType = 103
MessageAnswer MessageType = 104
MessageConnect MessageType = 110
MessageJoin MessageType = 111
MessageQuit MessageType = 112
@ -24,6 +27,8 @@ func (t MessageType) String() string {
return "Binary"
case MessagePing:
return "Ping"
case MessagePong:
return "Pong"
case MessageCall:
return "Call"
case MessageAnswer:
@ -45,6 +50,6 @@ func (t MessageType) String() string {
case MessageChat:
return "Chat"
default:
return "Unknown"
return fmt.Sprintf("%d?", t)
}
}

66
pkg/web/public/assets/js/harmony.js

@ -31,12 +31,16 @@ var audioTrack;
var shownPTTHelp = false;
var muteOnMouseUp = true;
var lastPing = 0;
var userPing = 0;
var userListStatus = 'Loading...';
var MessageBinary = 2;
var MessagePing = 100;
var MessageCall = 101;
var MessageAnswer = 102;
var MessagePing = 101;
var MessagePong = 102;
var MessageCall = 103;
var MessageAnswer = 104;
var MessageConnect = 110;
var MessageJoin = 111;
var MessageQuit = 112;
@ -163,7 +167,7 @@ $(document).ready(function () {
return;
}
w(MessagePing, "ping");
pingServer();
}, 15000);
if (printStats) {
@ -336,9 +340,12 @@ function Connect() {
clearTimeout(reconnectTimeout);
}
userListStatus = "";
updateUserStatus();
w(MessageNick, nickname);
updateUserStatus();
pingServer();
};
socket.onmessage = function (e) {
if (ReconnectDelay > 0) {
@ -352,7 +359,13 @@ function Connect() {
p.M = atob(p.M);
}
if (p.T == MessageAnswer) {
if (p.T == MessagePong) {
if (parseInt(p.M, 10) == lastPing) {
userPing = Date.now() - lastPing;
updateUserStatus();
}
} else if (p.T == MessageAnswer) {
if (p.PC === undefined || p.PC > peerConnections.length) {
return;
}
@ -376,7 +389,7 @@ function Connect() {
return;
}
Log( escapeEntities(p.N) + " quit #lobby voice chat");
Log(escapeEntities(p.N) + " quit #lobby voice chat");
} else if (p.T == MessageDisconnect) {
if (p.N === undefined) {
return;
@ -390,21 +403,10 @@ function Connect() {
Log("<" + escapeEntities(p.N) + "> " + escapeEntities(p.M));
} else if (p.T == MessageUsers) {
var usersconnected = 0;
var usersvoice = 0;
var u = JSON.parse(p.M);
for (let i = 0; i < u.length; i++) {
usersconnected++;
if (u[i].V) {
usersvoice++;
}
// TODO: Parse
}
userListStatus = "Users: " + usersconnected + " - Voice chatting: " + usersvoice;
updateUserStatus();
}
} else {
// TODO Binary data
@ -432,6 +434,11 @@ function Connect() {
};
}
function pingServer() {
lastPing = Date.now();
w(MessagePing, lastPing);
}
function webSocketReady() {
return (socket !== null && socket.readyState === 1);
}
@ -464,6 +471,8 @@ function StartPTT() {
ptt = true;
$("#voiceptt").html('Transmitting...');
var sender = peerConnections[0].getSenders()[0];
sender.replaceTrack(audioTrack);
@ -477,6 +486,8 @@ function StopPTT() {
ptt = false;
$("#voiceptt").html('Push-To-Talk');
var sender = peerConnections[0].getSenders()[0];
sender.replaceTrack(null);
@ -484,11 +495,20 @@ function StopPTT() {
}
function updateUserStatus() {
if (ptt) {
$('#userstatus').html('<b>Transmitting</b>');
} else {
$('#userstatus').html(userListStatus);
var out = '';
if (userPing > 0) {
out += userPing + 'ms ping';
}
if (userListStatus != '') {
if (out != '') {
out += '<br>';
}
out += userListStatus;
}
$('#userstatus').html(out);
}
function w(t, m) {

2
pkg/web/web.go

@ -122,7 +122,7 @@ func (w *WebInterface) handleRead(c *Client) {
// TODO Binary message
continue
case MessagePing:
// TODO Handle ping
c.Out <- &Message{T: MessagePong, M: msg.M}
case MessageCall:
answer, err := w.answerRTC(c, msg.PC, msg.M)
if err != nil {

Loading…
Cancel
Save