[test] peer fix

This commit is contained in:
kwaroran
2023-11-22 19:17:09 +09:00
parent 043ac92260
commit 6765fdc50c

View File

@@ -25,9 +25,14 @@ export async function createMultiuserRoom(){
roomId + "-risuai-multiuser" roomId + "-risuai-multiuser"
) )
alertWait("Waiting for peerserver to connect...")
let open = false
peer.on('open', function(id) {
open = true
});
peer.on('connection', function(conn) { peer.on('connection', function(conn) {
connections.push(conn) connections.push(conn)
console.log("new connection") console.log("new connection", conn)
function requestChar(){ function requestChar(){
const db = get(DataBase) const db = get(DataBase)
const selectedCharId = get(selectedCharID) const selectedCharId = get(selectedCharID)
@@ -41,7 +46,6 @@ export async function createMultiuserRoom(){
data: char data: char
}); });
} }
requestChar()
conn.on('data', function(data:ReciveData) { conn.on('data', function(data:ReciveData) {
if(data.type === 'request-char'){ if(data.type === 'request-char'){
@@ -60,6 +64,9 @@ export async function createMultiuserRoom(){
} }
}); });
}); });
while(!open){
await sleep(100)
}
alertNormal("Room ID: " + roomId) alertNormal("Room ID: " + roomId)
@@ -86,37 +93,42 @@ export async function joinMultiuserRoom(){
v4() + "-risuai-multiuser-join" v4() + "-risuai-multiuser-join"
) )
conn = peer.connect(roomId + '-risuai-multiuser'); alertWait("Waiting for peerserver to connect...")
alertWait("Waiting for host to accept connection")
let open = false let open = false
conn.on('open', function() { conn.on('open', function() {
conn.on('data', function(data:ReciveData) { alertWait("Waiting for host to accept connection")
switch(data.type){ conn = peer.connect(roomId + '-risuai-multiuser');
case 'receive-char':{ open = true
//create temp character conn.send({
const db = get(DataBase) type: 'request-char'
const cha = data.data })
cha.chaId = '§temp' });
cha.chatPage = 0
const ind = findCharacterIndexbyId('§temp') conn.on('data', function(data:ReciveData) {
const selectedcharIndex = get(selectedCharID) switch(data.type){
if(ind === -1){ case 'receive-char':{
db.characters.push(cha) //create temp character
} const db = get(DataBase)
else{ const cha = data.data
db.characters[ind] = cha cha.chaId = '§temp'
} cha.chatPage = 0
const tempInd = findCharacterIndexbyId('§temp') const ind = findCharacterIndexbyId('§temp')
if(selectedcharIndex !== tempInd){ const selectedcharIndex = get(selectedCharID)
selectedCharID.set(tempInd) if(ind === -1){
} db.characters.push(cha)
setDatabase(db)
break
} }
else{
db.characters[ind] = cha
}
const tempInd = findCharacterIndexbyId('§temp')
if(selectedcharIndex !== tempInd){
selectedCharID.set(tempInd)
}
setDatabase(db)
break
} }
}); }
}); });
let waitTime = 0 let waitTime = 0
@@ -128,6 +140,7 @@ export async function joinMultiuserRoom(){
return return
} }
} }
alertNormal("Connected")
return { return {
peer, roomId, conn peer, roomId, conn