This repository was archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
websocket test page for sanity (#94)
* websocket test page for sanity
- Loading branch information
1 parent
0035330
commit 9dde89d
Showing
4 changed files
with
336 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package net.wasdev.gameon.room; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Servlet implementation class Health | ||
*/ | ||
@WebServlet("/health") | ||
public class Health extends HttpServlet { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse | ||
* response) | ||
*/ | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
response.setContentType("application/json"); | ||
|
||
PrintWriter out = response.getWriter(); | ||
out.println("{\"status\": \"UP\"}"); | ||
} | ||
|
||
/** | ||
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse | ||
* response) | ||
*/ | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
doGet(request, response); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,82 @@ | ||
<!-- | ||
Copyright (c) 2015 IBM Corp. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Game On!</title> | ||
<link rel="icon" href="favicon.ico" type="image/x-icon"> | ||
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"> | ||
<style> | ||
input:disabled { background-color: gray; } | ||
.callout { | ||
border: 1px solid silver; | ||
margin: 1em; | ||
padding: .5em; | ||
} | ||
#socketUrl { | ||
color: blue; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<body style="width: 80%"> | ||
|
||
<h1>Your room is running! </h1> | ||
|
||
<div class="callout"> | ||
<dl> | ||
<li><b>WebSocket Endpoint: <span id="socketUrl"></span></b></li> | ||
<li>Health Check (REST): <a href="/health"><span id="healthUrl"></span></a></li> | ||
</dl> | ||
</div> | ||
|
||
<p>What next?</p> | ||
|
||
<ul> | ||
<li>Use the bottom half of this page for the equivalent of poking your room with a stick</li> | ||
<li>Use the <code>/listmyrooms</code> command in First Room to find your live room in the game</li> | ||
<li>Add custom commands and items to your room (see org.gameontext.sample.RoomImplementation)</li> | ||
<li>Scale your service -- does that change how you manage state?</li> | ||
<li>Check out other <a href="https://book.gameontext.org/walkthroughs/createMore.html">Advanced Adventures</a> | ||
<li>Learn about the game's <a href="https://book.gameontext.org/microservices/">Application architecture</a> | ||
</ul> | ||
|
||
<p> Go play! Game On!</p> | ||
|
||
<hr /> | ||
<h2>Test out the rooms </h2> | ||
<button id="RecRoom" onclick="changeTo('RecRoom')">RecRoom</button> | ||
<button id="Basement" onclick="changeTo('Basement')">Basement</button> | ||
<button id="MugRoom" onclick="changeTo('MugRoom')">MugRoom</button> | ||
|
||
<p>If your room pays attention to the room id, update this to match the target id</p> | ||
<input type="text" id="roomId" size="50" value="TheGeneratedIdOfThisRoom" /> | ||
|
||
<button id="connectButton" onclick="connect()">connect</button><button id="disconnectButton" onclick="disconnect()" disabled>disconnect</button> | ||
|
||
<h3>Protocol operations:</h3> | ||
<button id="helloButton" onclick="hello()" disabled>roomHello</button><button id="goodbyeButton" onclick="goodbye()" disabled>roomGoodbye</button> | ||
<button id="joinButton" onclick="join()" disabled>roomJoin</button><button id="partButton" onclick="part()" disabled>roomPart</button> | ||
|
||
<h3>User initiated commands:</h3> | ||
<form id="simpleForm"> | ||
<input type="text" id="inputmessage" size="50" placeholder="type something and press ENTER, type 'clear' to clear" disabled/> | ||
</form> | ||
<h4>Activity:</h4> | ||
<div id="response" style="width: 90%; margin: 0 auto; border: 1px solid gray; height: 20rem; max-height: 40rem; overflow-y: scroll"></div> | ||
|
||
Room is up and ready to go! | ||
<script type="text/javascript" src="websocketClient.js"></script> | ||
|
||
</html> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
var websocket = null; | ||
var websocketRoot = "wss://" + window.document.location.host + "/rooms/ws/"; | ||
var websocketUrl = websocketRoot + "Basement"; | ||
var healthUrl = "https://" + window.document.location.host + "/health"; | ||
|
||
var inputMessage = document.getElementById("inputmessage"); | ||
var connectButton = document.getElementById("connectButton"); | ||
var disconnectButton = document.getElementById("disconnectButton"); | ||
var helloButton = document.getElementById("helloButton"); | ||
var goodbyeButton = document.getElementById("goodbyeButton"); | ||
var joinButton = document.getElementById("joinButton"); | ||
var partButton = document.getElementById("partButton"); | ||
var roomId = document.getElementById("roomId"); | ||
var response = document.getElementById("response"); | ||
|
||
console.log("buttons %o %o %o %o %o %o %o", | ||
inputMessage, connectButton, disconnectButton, helloButton, goodbyeButton, joinButton, partButton); | ||
|
||
document.getElementById("socketUrl").innerHTML = websocketUrl; | ||
document.getElementById("healthUrl").innerHTML = healthUrl; | ||
|
||
function changeTo(target) { | ||
websocketUrl = websocketRoot + target; | ||
document.getElementById("socketUrl").innerHTML = websocketUrl; | ||
} | ||
|
||
function connect() { | ||
console.log("connect %o", websocket); | ||
|
||
if ( websocket === null ) { | ||
response.innerHTML = ""; | ||
connectButton.disabled = true; | ||
|
||
websocket = new WebSocket(websocketUrl); | ||
websocket.onerror = function(event) { | ||
response.innerHTML += "Error: " + event.data + "<br />"; | ||
}; | ||
|
||
websocket.onopen = function(event) { | ||
response.innerHTML += "Connection established<br />"; | ||
|
||
disconnectButton.disabled = false; | ||
helloButton.disabled = false; | ||
goodbyeButton.disabled = false; | ||
joinButton.disabled = false; | ||
partButton.disabled = false; | ||
inputMessage.disabled = false; | ||
}; | ||
|
||
websocket.onclose = function(event) { | ||
websocket = null; | ||
response.innerHTML += "Connection closed: " + event.code + "<br />"; | ||
connectButton.disabled = false; | ||
disconnectButton.disabled = true; | ||
helloButton.disabled = true; | ||
goodbyeButton.disabled = true; | ||
joinButton.disabled = true; | ||
partButton.disabled = true; | ||
inputMessage.disabled = true; | ||
}; | ||
|
||
websocket.onmessage = function(event) { | ||
response.innerHTML += "← " + event.data + "<br />"; | ||
var isScrolledToBottom = response.scrollHeight - response.clientHeight <= response.scrollTop + 1; | ||
console.log("isScrolledToBottom: %o", isScrolledToBottom); | ||
if(!isScrolledToBottom) { | ||
response.scrollTop = response.scrollHeight - response.clientHeight; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
function sendSocket(payload) { | ||
console.log("sendSocket %o, %o", websocket, payload); | ||
if ( websocket !== null ) { | ||
response.innerHTML += "→ " + payload + "<br />"; | ||
websocket.send(payload); | ||
} | ||
} | ||
|
||
function disconnect() { | ||
console.log("disconnect %o", websocket); | ||
|
||
if ( websocket !== null ) { | ||
websocket.close(); | ||
|
||
disconnectButton.disabled = true; | ||
helloButton.disabled = true; | ||
goodbyeButton.disabled = true; | ||
joinButton.disabled = true; | ||
partButton.disabled = true; | ||
response.disabled = true; | ||
} | ||
} | ||
|
||
function hello() { | ||
console.log("hello %o", websocket); | ||
// roomHello,<roomId>,{ | ||
// "username": "username", | ||
// "userId": "<userId>", | ||
// "version": 1|2 | ||
// } | ||
var roomHello = { | ||
"username": "webtest", | ||
"userId": "dummyId", | ||
"version": 2 | ||
}; | ||
|
||
sendSocket("roomHello," + roomId.value + "," + JSON.stringify(roomHello)); | ||
} | ||
|
||
function goodbye() { | ||
console.log("goodbye %o", websocket); | ||
// roomGoodbye,<roomId>,{ | ||
// "username": "username", | ||
// "userId": "<userId>" | ||
// } | ||
var roomGoodbye = { | ||
"username": "webtest", | ||
"userId": "dummyId" | ||
}; | ||
|
||
sendSocket("roomGoodbye," + roomId.value + "," + JSON.stringify(roomGoodbye)); | ||
} | ||
|
||
function join() { | ||
console.log("join %o", websocket); | ||
// roomJoin,<roomId>,{ | ||
// "username": "username", | ||
// "userId": "<userId>", | ||
// "version": 2 | ||
// } | ||
var roomJoin = { | ||
"username": "webtest", | ||
"userId": "dummyId", | ||
"version": 2 | ||
}; | ||
|
||
sendSocket("roomJoin," + roomId.value + "," + JSON.stringify(roomJoin)); | ||
} | ||
|
||
function part() { | ||
console.log("part %o", websocket); | ||
// roomPart,<roomId>,{ | ||
// "username": "username", | ||
// "userId": "<userId>" | ||
// } | ||
var roomPart = { | ||
"username": "webtest", | ||
"userId": "dummyId" | ||
}; | ||
|
||
sendSocket("roomPart," + roomId.value + "," + JSON.stringify(roomPart)); | ||
} | ||
|
||
function emulateClient() { | ||
console.log("emulateClient %o", websocket); | ||
// room,<roomId>,{ | ||
// "username": "username", | ||
// "userId": "<userId>" | ||
// "content": "<message>" | ||
// } | ||
var message = { | ||
"username": "webtest", | ||
"userId": "dummyId" | ||
}; | ||
var txt = inputMessage.value; | ||
inputMessage.value=""; | ||
|
||
if ( txt.indexOf("clear") >= 0) { | ||
response.innerHTML=""; | ||
} else { | ||
message.content = txt; | ||
sendSocket("room," + roomId.value + "," + JSON.stringify(message)); | ||
} | ||
} | ||
|
||
function submit(event) { | ||
event.preventDefault(); | ||
emulateClient(); | ||
} | ||
document.getElementById("simpleForm").addEventListener("submit", submit, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters