Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
websocket test page for sanity (#94)
Browse files Browse the repository at this point in the history
* websocket test page for sanity
  • Loading branch information
Erin Schnabel authored and BarDweller committed Aug 29, 2018
1 parent 0035330 commit 9dde89d
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 5 deletions.
56 changes: 56 additions & 0 deletions room-app/src/main/java/net/wasdev/gameon/room/Health.java
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);
}

}
81 changes: 76 additions & 5 deletions room-app/src/main/webapp/index.html
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>
182 changes: 182 additions & 0 deletions room-app/src/main/webapp/websocketClient.js
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 += "&larr; " + 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 += "&rarr; " + 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);
22 changes: 22 additions & 0 deletions room-wlpcfg/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,26 @@ if [ "$ETCDCTL_ENDPOINT" != "" ]; then
wget https://github.com/ibm-messaging/message-hub-samples/raw/master/java/message-hub-liberty-sample/lib-message-hub/messagehub.login-1.0.0.jar
fi

if [ -f /etc/cert/cert.pem ]; then
echo "Building keystore/truststore from cert.pem"
echo "-creating dir"
mkdir -p ${SERVER_PATH}/resources/security
echo "-cd dir"
cd ${SERVER_PATH}/resources/
echo "-importing jvm truststore to server truststore"
keytool -importkeystore -srckeystore $JAVA_HOME/lib/security/cacerts -destkeystore security/truststore.jks -srcstorepass changeit -deststorepass truststore
echo "-converting pem to pkcs12"
openssl pkcs12 -passin pass:keystore -passout pass:keystore -export -out cert.pkcs12 -in /etc/cert/cert.pem
echo "-importing pem to truststore.jks"
keytool -import -v -trustcacerts -alias default -file /etc/cert/cert.pem -storepass truststore -keypass keystore -noprompt -keystore security/truststore.jks
echo "-creating dummy key.jks"
keytool -genkey -storepass testOnlyKeystore -keypass wefwef -keyalg RSA -alias endeca -keystore security/key.jks -dname CN=rsssl,OU=unknown,O=unknown,L=unknown,ST=unknown,C=CA
echo "-emptying key.jks"
keytool -delete -storepass testOnlyKeystore -alias endeca -keystore security/key.jks
echo "-importing pkcs12 to key.jks"
keytool -v -importkeystore -srcalias 1 -alias 1 -destalias default -noprompt -srcstorepass keystore -deststorepass testOnlyKeystore -srckeypass keystore -destkeypass testOnlyKeystore -srckeystore cert.pkcs12 -srcstoretype PKCS12 -destkeystore security/key.jks -deststoretype JKS
echo "done"
cd ${SERVER_PATH}
fi

exec /opt/ol/wlp/bin/server run defaultServer

0 comments on commit 9dde89d

Please sign in to comment.