Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic websocket stuff was pretty easy #68
- Loading branch information
1 parent
b254798
commit 8996d10
Showing
7 changed files
with
108 additions
and
3 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
Binary file not shown.
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
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
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,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); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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> |