私は今通信対戦ができるボードゲームを作っているのですが、過去の質問でサンプルプログラムをもらいました。そのサンプルでは遷移の指定が書かれていなかったので、遷移の指定を追加したりと自分なりに模索していたのですが、元々ページ遷移指定の条件分岐に使いたかったdocument.location
が使えず仕方なく
window.open
でウインドウ生成ありのページ遷移で動作確認を行おうとしたところ、
ポップアップブロックというものが出てきてしまいました。
一体何が原因なのかわからないのでご教授願いたいです。
実現したいこと
下記に表示するサンプルにウインドウ生成なしで条件分岐させたいです。
最終的のこのサンプルは本プログラムに生かしていこうと思っています。
発生している問題・エラーメッセージ
window.openで開こうとした際にいかのような表示が出ます。ポップアップを使った記憶はないです。
該当のソースコード
今回のサンプルです。
js
1// app.js 2const express = require("express") 3const { createServer } = require("http") 4const { Server } = require("socket.io") 5 6const app = express() 7const httpServer = createServer(app) 8const io = new Server(httpServer, { /* options */ }) 9const port = 3000 10 11app.set("view engine", "pug") 12 13app.get("/", (req, res) => { 14 res.render("index", { title: "Hey", message: "Hello there!" }) 15}) 16 17app.get("/window1", (req, res) => { 18 res.render("window1") 19}) 20 21app.get("/1", (req, res) => { 22 res.render("test1") 23}) 24 25app.get("/2", (req, res) => { 26 res.render("test2") 27}) 28 29const socketPerRoom = 2 30let socketCount = 0 31 32io.on("connection", (socket) => { 33 console.log(`connection socket.id: ${socket.id}`) 34 socket.on("join", () => { 35 socketCount++ 36 const room = `room${Math.ceil(socketCount / socketPerRoom)}` 37 const num = socketCount % 2 38 // ソケットをルームに追加する 39 socket.join(room) 40 console.log(`join socket.id: ${socket.id}, socket.rooms: ${JSON.stringify([...socket.rooms])}`) 41 // ルーム全体に送信する(最大2つのソケット) 42 io.to(room).emit("joined", socket.id, room, num) 43 }) 44}) 45 46httpServer.listen(port, () => { 47 console.log(`Example app listening on port ${port}`) 48})
pug
1//- views/index.pug 2html 3 head 4 title= title 5 script(src="https://cdn.socket.io/4.4.1/socket.io.min.js", integrity="sha384-fKnu0iswBIqkjxrhQCTZ7qlLHOFEgNkRmK2vaO/LbTZSXdJfAu6ewRBdwHPhBo/H", crossorigin="anonymous") 6 body 7 h1= message 8 input(type="button", value="join", id="button1") 9 ul(id="ul1" style="list-style: none;") 10 script. 11 window.addEventListener("DOMContentLoaded", () => { 12 const button1 = document.querySelector("#button1") 13 button1.addEventListener("click", () => { 14 15 //- Socket.IO接続 16 const socket = io() 17 socket.on("connect", () => { 18 //- 接続した時のイベント 19 //- ソケットのIDをコンソールと画面のリストに出力する 20 console.log(`connect: ${socket.id}`) 21 const li1 = document.createElement('li') 22 //- li1.textContent = `connect socket.id: ${socket.id}` 23 const ul1 = document.querySelector("#ul1") 24 ul1.append(li1) 25 }) 26 socket.on("disconnect", () => { 27 console.log(`disconnect: ${socket.id}`) 28 }) 29 socket.on("joined", (socketId, room, flg) => { 30 //- ルームに追加した時のイベント 31 //- ルームに追加されたソケットのIDとルームの名前をコンソールと画面のリストに出力する 32 console.log(`joined socketId: ${socketId}, room: ${room}`) 33 const li1 = document.createElement('li') 34 //- li1.textContent = `joined socketId: ${socketId}, room: ${room}, flg: ${flg}` 35 const ul1 = document.querySelector("#ul1") 36 ul1.append(li1) 37 38 if(flg == 1){ 39 window.open("/1", "_blank") 40 //- window.close() 41 }else if(flg == 0){ 42 window.open("/2", "_blank") 43 //- window.close() 44 } 45 }) 46 //- サーバーに送信する(ルームに追加するためのイベント) 47 socket.emit("join") 48 }) 49 })
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。