Added notification sound.

This commit is contained in:
retoor 2025-01-28 17:24:10 +01:00
parent 4f1a48c197
commit 5aee606d5d
8 changed files with 161 additions and 144 deletions

View File

@ -53,10 +53,10 @@ class Messages {
class Room {
name = null
messages = []
constructor(name){
constructor(name) {
this.name = name
}
setMessages(list){
setMessages(list) {
}
@ -66,7 +66,7 @@ class Room {
class InlineAppElement extends HTMLElement {
constructor(){
constructor() {
// this.
}
@ -80,25 +80,25 @@ class Page {
class RESTClient {
debug = false
async get(url, params){
async get(url, params) {
params = params ? params : {}
const encodedParams = new URLSearchParams(params);
if(encodedParams)
if (encodedParams)
url += '?' + encodedParams
const response = await fetch(url,{
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
const result = await response.json()
if(this.debug){
console.debug({url:url,params:params,result:result})
if (this.debug) {
console.debug({ url: url, params: params, result: result })
}
return result
}
async post(url, data) {
const response = await fetch(url,{
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
@ -107,8 +107,8 @@ class RESTClient {
});
const result = await response.json()
if(this.debug){
console.debug({url:url,params:params,result:result})
if (this.debug) {
console.debug({ url: url, params: params, result: result })
}
return result
}
@ -117,17 +117,17 @@ const rest = new RESTClient()
class EventHandler {
constructor(){
constructor() {
this.subscribers = {}
}
addEventListener(type,handler){
if(!this.subscribers[type])
addEventListener(type, handler) {
if (!this.subscribers[type])
this.subscribers[type] = []
this.subscribers[type].push(handler)
}
emit(type,...data){
if(this.subscribers[type])
this.subscribers[type].forEach(handler=>handler(...data))
emit(type, ...data) {
if (this.subscribers[type])
this.subscribers[type].forEach(handler => handler(...data))
}
}
@ -136,22 +136,22 @@ class Chat extends EventHandler {
constructor() {
super()
this._url = window.location.hostname == 'localhost' ? 'ws://localhost/chat.ws' : 'wss://' + window.location.hostname +'/chat.ws'
this._url = window.location.hostname == 'localhost' ? 'ws://localhost/chat.ws' : 'wss://' + window.location.hostname + '/chat.ws'
this._socket = null
this._wait_connect = null
this._promises = {}
}
connect(){
if(this._wait_connect)
connect() {
if (this._wait_connect)
return this._wait_connect
const me = this
return new Promise(async (resolve,reject)=>{
return new Promise(async (resolve, reject) => {
me._wait_connect = resolve
me._socket = new WebSocket(me._url)
console.debug("Connecting..")
me._socket.onconnect = ()=>{
me._socket.onconnect = () => {
me._connected()
me._wait_socket(me)
}
@ -161,15 +161,15 @@ class Chat extends EventHandler {
generateUniqueId() {
return 'id-' + Math.random().toString(36).substr(2, 9); // Example: id-k5f9zq7
}
call(method,...args){
call(method, ...args) {
const me = this
return new Promise(async (resolve,reject)=>{
try{
const command = {method:method,args:args,message_id:me.generateUniqueId()}
return new Promise(async (resolve, reject) => {
try {
const command = { method: method, args: args, message_id: me.generateUniqueId() }
me._promises[command.message_id] = resolve
await me._socket.send(JSON.stringify(command))
}catch(e){
} catch (e) {
reject(e)
}
})
@ -178,11 +178,11 @@ class Chat extends EventHandler {
const me = this
this._socket.onmessage = (event) => {
const message = JSON.parse(event.data)
if(message.message_id && me._promises[message.message_id]){
if (message.message_id && me._promises[message.message_id]) {
me._promises[message.message_id](message)
delete me._promises[message.message_id]
}else{
me.emit("message",me, message)
} else {
me.emit("message", me, message)
}
//const room = this.rooms.find(room=>room.name == message.room)
//if(!room){
@ -191,14 +191,14 @@ class Chat extends EventHandler {
this._socket.onclose = (event) => {
me._wait_socket = null
me._socket = null
me.emit('close',me)
me.emit('close', me)
}
}
async privmsg(room, text) {
await rest.post("/api/privmsg",{
room:room,
text:text
await rest.post("/api/privmsg", {
room: room,
text: text
})
}
@ -212,7 +212,7 @@ class Socket extends EventHandler {
connectPromises = []
constructor() {
super()
this.url = window.location.hostname == 'localhost' ? 'ws://localhost:8081/rpc.ws' : 'wss://' + window.location.hostname +'/rpc.ws'
this.url = window.location.hostname == 'localhost' ? 'ws://localhost:8081/rpc.ws' : 'wss://' + window.location.hostname + '/rpc.ws'
this.ensureConnection()
}
_camelToSnake(str) {
@ -235,26 +235,26 @@ class Socket extends EventHandler {
);
return proxy
}
ensureConnection(){
ensureConnection() {
return this.connect()
}
generateUniqueId() {
return 'id-' + Math.random().toString(36).substr(2, 9);
}
connect(){
connect() {
const me = this
if(!this.isConnected && !this.isConnecting){
if (!this.isConnected && !this.isConnecting) {
this.isConnecting = true
}else if (this.isConnecting){
return new Promise((resolve,reject)=>{
} else if (this.isConnecting) {
return new Promise((resolve, reject) => {
me.connectPromises.push(resolve)
})
}else if(this.isConnected){
return new Promise((resolve,reject)=>{
} else if (this.isConnected) {
return new Promise((resolve, reject) => {
resolve(me)
})
}
return new Promise((resolve,reject)=>{
return new Promise((resolve, reject) => {
me.connectPromises.push(resolve)
const ws = new WebSocket(this.url)
@ -265,41 +265,41 @@ class Socket extends EventHandler {
ws.onmessage = (event) => {
me.onData(JSON.parse(event.data))
}
ws.onclose = (event) =>{
ws.onclose = (event) => {
me.onClose()
}
me.connectPromises.forEach(resolve=>{
me.connectPromises.forEach(resolve => {
resolve(me)
})
}
})
}
onData(data){
if(data.callId){
onData(data) {
if (data.callId) {
this.emit(data.callId, data.data)
}
if(data.channel_uid){
this.emit(data.channel_uid,data.data)
this.emit("channel-message",data)
if (data.channel_uid) {
this.emit(data.channel_uid, data.data)
this.emit("channel-message", data)
}
}
async sendJson(data){
return await this.connect().then((api)=>{
async sendJson(data) {
return await this.connect().then((api) => {
api.ws.send(JSON.stringify(data))
})
}
async call(method,...args){
const call= {
async call(method, ...args) {
const call = {
callId: this.generateUniqueId(),
method: method,
args: args
}
const me = this
return new Promise(async(resolve,reject)=>{
me.addEventListener(call.callId,(data)=>{
return new Promise(async (resolve, reject) => {
me.addEventListener(call.callId, (data) => {
resolve(data)
})
await me.sendJson(call)
@ -307,11 +307,11 @@ class Socket extends EventHandler {
})
}
onClose(){
onClose() {
console.info("Connection lost. Reconnecting.")
this.isConnected = false
this.isConnecting = false
this.ensureConnection().then(()=>{
this.ensureConnection().then(() => {
console.info("Reconnected.")
})
}
@ -323,6 +323,21 @@ class App extends EventHandler {
rest = rest
ws = null
rpc = null
sounds = ["/audio/soundfx.d_beep3.mp3"]
playSound(soundIndex) {
if (!soundIndex)
soundIndex = 0
const player = new Audio(this.sounds[soundIndex]);
player.play()
.then(() => {
console.debug("Gave sound notification")
})
.catch((error) => {
console.error("Notification failed:", error);
});
}
constructor() {
super()
this.rooms.push(new Room("General"))
@ -330,20 +345,20 @@ class App extends EventHandler {
this.rpc = this.ws.client
const me = this
this.ws.addEventListener("channel-message", (data) => {
me.emit(data.channel_uid,data)
me.emit(data.channel_uid, data)
})
}
async benchMark(times,message) {
if(!times)
async benchMark(times, message) {
if (!times)
times = 100
if(!message)
if (!message)
message = "Benchmark Message"
let promises = []
const me = this
for(let i = 0; i < times; i++){
promises.push(this.rpc.getChannels().then(channels=>{
channels.forEach(channel=>{
me.rpc.sendMessage(channel.uid,`${message} ${i}`).then(data=>{
for (let i = 0; i < times; i++) {
promises.push(this.rpc.getChannels().then(channels => {
channels.forEach(channel => {
me.rpc.sendMessage(channel.uid, `${message} ${i}`).then(data => {
})
})

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -51,6 +51,7 @@ class ChatWindowElement extends HTMLElement {
})
const me = this
channelElement.addEventListener("message",(message)=>{
app.playSound(0)
message.detail.element.scrollIntoView()
})

View File

@ -66,6 +66,7 @@ class MessageListElement extends HTMLElement {
this.messageEventSchedule.delay(() => {
me.dispatchEvent(new CustomEvent("message", {detail:obj,bubbles:true}))
})