Skip to content

Commit

Permalink
basic websocket stuff was pretty easy #68
Browse files Browse the repository at this point in the history
  • Loading branch information
zachtaylor committed Jul 28, 2013
1 parent b254798 commit 8996d10
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 3 deletions.
1 change: 1 addition & 0 deletions .classpath
Expand Up @@ -23,5 +23,6 @@
<classpathentry kind="lib" path="lib/jetty/servlet-api-3.0.jar"/>
<classpathentry kind="lib" path="lib/org.zachtaylor.myna-0.0.x.jar"/>
<classpathentry kind="lib" path="lib/jnodalxml-0.0.x.jar"/>
<classpathentry kind="lib" path="lib/jetty/jetty-websocket-8.1.12.v20130726.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added lib/jetty/jetty-websocket-8.1.12.v20130726.jar
Binary file not shown.
22 changes: 21 additions & 1 deletion src/org/jpokemon/manager/PlayerManager.java
Expand Up @@ -11,9 +11,10 @@
import java.util.Map;
import java.util.Queue;

import org.jpokemon.server.JPokemonServer;
import org.jpokemon.manager.component.OverworldActivity;
import org.jpokemon.manager.message.Message;
import org.jpokemon.server.JPokemonServer;
import org.jpokemon.server.JPokemonWebSocket;
import org.jpokemon.trainer.Player;
import org.jpokemon.trainer.PokemonTrainer;
import org.json.JSONArray;
Expand Down Expand Up @@ -57,6 +58,24 @@ public static void activityRequest(JSONObject request) throws ServiceException {
}
}

public static void dispatchRequest(JPokemonWebSocket socket, JSONObject request) throws JSONException, ServiceException {
Player player = connections.get(socket);
String action = request.getString("action");

// TODO : Refactor all of this shit
if (action.equals("login")) {
String name = loadPlayer(request.getString("name"));
socket.sendLog("Loaded with name: " + name);
}
else if (action.equals("create")) {
String name = createPlayer(request.getString("name"), request.getString("rival"));
socket.sendLog("Loaded with name: " + name);
}
else {
// TODO : do stuff with the player
}
}

public static Player getPlayer(String id) {
if (players.get(id) == null)
throw new IllegalArgumentException("Could not retrieve PlayerID: " + id);
Expand Down Expand Up @@ -162,6 +181,7 @@ private static String getUniquePlayerName(String attempt) {
}

private static Map<String, Player> players = new HashMap<String, Player>();
private static Map<JPokemonWebSocket, Player> connections = new HashMap<JPokemonWebSocket, Player>();
private static Map<Player, Activity> activities = new HashMap<Player, Activity>();
private static Map<Player, Queue<Message>> messageQueues = new HashMap<Player, Queue<Message>>();

Expand Down
3 changes: 1 addition & 2 deletions src/org/jpokemon/server/JPokemonServer.java
Expand Up @@ -36,8 +36,7 @@ public static void main(String[] args) throws Exception {
ServletContextHandler root = new ServletContextHandler();
root.setContextPath("/");
root.addServlet(new ServletHolder(new FileServlet()), "/*");
root.addServlet(new ServletHolder(new PlayerServlet()), PlayerServlet.URL_PATH);
root.addServlet(new ServletHolder(new LoginServlet()), LoginServlet.URL_PATH);
root.addServlet(new ServletHolder(new JPokemonWebSocketServlet()), "/socket");

// Instantiate the server on the specified port
logger.info("Server starting on port " + port);
Expand Down
53 changes: 53 additions & 0 deletions src/org/jpokemon/server/JPokemonWebSocket.java
@@ -0,0 +1,53 @@
package org.jpokemon.server;

import java.io.IOException;

import org.eclipse.jetty.websocket.WebSocket;
import org.jpokemon.manager.PlayerManager;
import org.jpokemon.manager.ServiceException;
import org.json.JSONException;
import org.json.JSONObject;

public class JPokemonWebSocket implements WebSocket.OnTextMessage {
private Connection connection;

@Override
public void onClose(int arg0, String arg1) {
}

@Override
public void onOpen(Connection c) {
connection = c;
}

@Override
public void onMessage(String arg0) {
try {
PlayerManager.dispatchRequest(this, new JSONObject(arg0));
} catch (JSONException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}

public void sendJson(JSONObject json) {
try {
connection.sendMessage(json.toString());
} catch (IOException e) {
e.printStackTrace();
}
}

public void sendLog(String message) {
JSONObject json = new JSONObject();

try {
json.put("action", "log");
json.put("message", message);
} catch (JSONException e) {
}

sendJson(json);
}
}
15 changes: 15 additions & 0 deletions src/org/jpokemon/server/JPokemonWebSocketServlet.java
@@ -0,0 +1,15 @@
package org.jpokemon.server;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;

public class JPokemonWebSocketServlet extends WebSocketServlet {
private static final long serialVersionUID = 1L;

@Override
public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) {
return new JPokemonWebSocket();
}
}
17 changes: 17 additions & 0 deletions web/index.html
@@ -0,0 +1,17 @@
<html>
<head>
<script type="text/javascript">
var ws = new WebSocket('ws://' + document.location.hostname + ':' + document.location.port + '/socket');
ws.onmessage = function(message) {
var json = JSON.parse(message.data);

if (json.action === 'log') {
console.log(json.message);
}
}
</script>
<head>
<body>
<h1> JPokemon loaded successfully I think</h1>
</body>
</html>

0 comments on commit 8996d10

Please sign in to comment.