[test] p2p

This commit is contained in:
kwaroran
2023-11-22 19:43:10 +09:00
parent 6765fdc50c
commit 7c74d6849e

View File

@@ -85,7 +85,6 @@ type ReciveData = ReciveFirst|RequestFirst
export async function joinMultiuserRoom(){ export async function joinMultiuserRoom(){
const roomId = await alertInput("Enter room id")
//join a room with webrtc //join a room with webrtc
alertWait("Loading...") alertWait("Loading...")
const peerJS = await importPeerJS(); const peerJS = await importPeerJS();
@@ -93,59 +92,58 @@ export async function joinMultiuserRoom(){
v4() + "-risuai-multiuser-join" v4() + "-risuai-multiuser-join"
) )
alertWait("Waiting for peerserver to connect...") peer.on('open', async (id) => {
const roomId = await alertInput("Enter room id")
alertWait("Waiting for peerserver to connect...")
let open = false let open = false
conn.on('open', function() { conn.on('open', function() {
alertWait("Waiting for host to accept connection") alertWait("Waiting for host to accept connection")
conn = peer.connect(roomId + '-risuai-multiuser'); conn = peer.connect(roomId + '-risuai-multiuser');
open = true open = true
conn.send({ conn.send({
type: 'request-char' type: 'request-char'
}) })
}); });
conn.on('data', function(data:ReciveData) { conn.on('data', function(data:ReciveData) {
switch(data.type){ switch(data.type){
case 'receive-char':{ case 'receive-char':{
//create temp character //create temp character
const db = get(DataBase) const db = get(DataBase)
const cha = data.data const cha = data.data
cha.chaId = '§temp' cha.chaId = '§temp'
cha.chatPage = 0 cha.chatPage = 0
const ind = findCharacterIndexbyId('§temp') const ind = findCharacterIndexbyId('§temp')
const selectedcharIndex = get(selectedCharID) const selectedcharIndex = get(selectedCharID)
if(ind === -1){ if(ind === -1){
db.characters.push(cha) db.characters.push(cha)
}
else{
db.characters[ind] = cha
}
const tempInd = findCharacterIndexbyId('§temp')
if(selectedcharIndex !== tempInd){
selectedCharID.set(tempInd)
}
setDatabase(db)
break
} }
else{ }
db.characters[ind] = cha });
}
const tempInd = findCharacterIndexbyId('§temp') let waitTime = 0
if(selectedcharIndex !== tempInd){ while(!open){
selectedCharID.set(tempInd) await sleep(100)
} waitTime += 100
setDatabase(db) if(waitTime > 10000){
break alertError("Connection timed out")
return
} }
} }
alertNormal("Connected")
}); });
let waitTime = 0
while(!open){
await sleep(100)
waitTime += 100
if(waitTime > 10000){
alertError("Connection timed out")
return
}
}
alertNormal("Connected")
return {
peer, roomId, conn
}
} }