0
0

More than 3 years have passed since last update.

対戦ゲームを作る。 その2

Last updated at Posted at 2021-07-14

概要

対戦ゲームを作る。
通信サーバーをテストする。
2つ、開いて、送信データが両方に送られれば成功。
chatとしても、利用可能なのです。
テスト中に、知らない人、来るかも。

サンプルコード


function getUniq() {
  var strong = 3;
  return new Date().getTime().toString(16) + Math.floor(strong * Math.random()).toString(16)
}
var id0 = getUniq();
var out = document.getElementById("out");
var src = document.getElementById("src");
src.value = "{\n";
src.value += "id: " + id0 + ",\n";
src.value += "state: 0,\n";
src.value += "iro: 0,\n";
src.value += "ban: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 1, 1, 1, 1, 1, 1, 2, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n";
src.value += "te: 0\n";
src.value += "}";
var url = "wss://ohiapp0.herokuapp.com/pub";
var ws = new WebSocket(url);
ws.onopen = function() {
  out.innerHTML += "<p>> CONNECTED</p>"; 
  //send("open");
};
ws.onmessage = function(e) {
  out.innerHTML += "<p style='color: blue;'>> RECV: " + e.data + "</p>";
  //var json = JSON.parse(e.data);
};
ws.onerror = function(e) {
  out.innerHTML += "<p style='color: red;'>> ERROR: " + e.data + "</p>";
};
function send() {
  var m = src.value;
  out.innerHTML += "<p>> SEND: " + m + "</p>";
  ws.send(m);
}




成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0