<!doctype html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">

<link href="https://unpkg.com/nes.css@latest/css/nes.min.css" rel="stylesheet" />

  <title>ついったー的なやつ</title>
  <style>

  </style>
</head>
<body class="bg-pink-50">

    <main class="mt-20">
        <div class="nes-container is-dark with-title">
            <p class="title">Tubuyakisakata</p>
          <section class="my-4">
            <div>
              <span class="text-sm font-bold text-center">つぶやきばしょ</span>
              <div class="p-2 mb-2">
                <span class="websocket-status nes-text is-primary">null</span>
              </div>
            </div>
          </section>
          <section class="my-4">
            <div class="p-2 rounded shadow">
              <div>
                <input
                    class="appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline mb-2 nes-input is-dark"
                    id="send-message"
                    type="text"
                    placeholder="メッセージを入力"
                >
              </div>
              <div class="text-right">
                <button id="send-button" class="py-2 px-4 rounded nes-btn is-primary">
                  メッセージ送信
                </button>
              </div>
            </div>
          </section>
          <section class="my-4">
            <div class="text-sm font-bold text-center">メッセージ一覧</div>
            <p id="messages">
                
              </p>
          </section>
        </div>
    </main>



<script>
 const URL = 'ws://localhost:3000/'
const PROTOCOL = 'ws-sample'

const socket = new WebSocket(URL, PROTOCOL)

// ------------------------------
// WebSocket イベント
// ------------------------------

// WebSocket が開通したら発火する
// socket.onopen = () => {} でも可
socket.addEventListener('open', (event) => {
  setWsStatus('つぶやいてくれたまえ')
})

// WebSocketサーバ からメッセージを受け取ったら発火する
// socket.onmessage = () => {} でも可
socket.addEventListener('message', ({ data }) => {
  setWsStatus('message: ' + data)
  appendMessage(data)
})

// WebSocketサーバ からエラーメッセージを受け取ったら発火する
// socket.onerror = () => {} でも可
socket.addEventListener('error', (event) => {
  setWsStatus('エラー発生だよ')
  console.log('error')
})

// WebSocket がクローズしたら発火する
// socket.onclose = () => {} でも可
socket.addEventListener('close', (event) => {
  setWsStatus('つぶやき終わり！')
  console.log('close')
})

// ------------------------------
// WebSocket メソッド
// ------------------------------
const sendMessage = (message) => {
  socket.send(message)
}

// ------------------------------
// DOM 操作
// ------------------------------

const sendMessageEl = document.querySelector('#send-message')
const sendButtonEl = document.querySelector('#send-button')
sendButtonEl.addEventListener('click', () => {
  const message = sendMessageEl.value
  sendMessage(message)
  sendMessageEl.value = ''
})

const setWsStatus = (text) => {
  const statusEl = document.querySelector('.websocket-status')
  statusEl.innerHTML = text
}

const createMessageEl = (text) => {
  return `<div class="rounded bg-white p-2 mb-2 text text-gray-600">${text}</div>`
}
const appendMessage = (text) => {
  const el = createMessageEl(text)
  document.querySelector('#messages').insertAdjacentHTML('afterend', el)
}
    
    </script>
</body>
</html>