-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
230 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
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,64 @@ | ||
/* | ||
* Copyright 2016 MovingBlocks | ||
* | ||
* 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 org.terasology.web.servlet; | ||
|
||
import java.io.IOException; | ||
|
||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.api.WebSocketAdapter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Manages one websocket session | ||
*/ | ||
public class EventSocket extends WebSocketAdapter { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(EventSocket.class); | ||
|
||
@Override | ||
public void onWebSocketConnect(Session session) { | ||
super.onWebSocketConnect(session); | ||
logger.info("Connected: " + session.getRemoteAddress()); | ||
trySend("Connected!"); | ||
} | ||
|
||
@Override | ||
public void onWebSocketText(String message) { | ||
super.onWebSocketText(message); | ||
} | ||
|
||
@Override | ||
public void onWebSocketClose(int statusCode, String reason) { | ||
super.onWebSocketClose(statusCode, reason); | ||
logger.info("Socket Closed: [" + statusCode + "] " + reason); | ||
} | ||
|
||
@Override | ||
public void onWebSocketError(Throwable cause) { | ||
super.onWebSocketError(cause); | ||
logger.error("Error", cause); | ||
} | ||
|
||
private void trySend(String message) { | ||
try { | ||
getSession().getRemote().sendString(message); | ||
} catch (IOException e) { | ||
logger.warn("Unable to send message!", e); | ||
} | ||
} | ||
} |
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,49 @@ | ||
/* | ||
* Copyright 2015 MovingBlocks | ||
* | ||
* 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 org.terasology.web.servlet; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.glassfish.jersey.server.mvc.Viewable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.terasology.web.version.VersionInfo; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
/** | ||
* Show the log html page. | ||
*/ | ||
@Path("/") | ||
public class LogServlet { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(LogServlet.class); | ||
|
||
@GET | ||
@Path("log") | ||
@Produces(MediaType.TEXT_HTML) | ||
public Viewable log() { | ||
logger.info("Requested logs as HTML"); | ||
ImmutableMap<Object, Object> dataModel = ImmutableMap.builder() | ||
.put("version", VersionInfo.getVersion()) | ||
.build(); | ||
return new Viewable("/log.ftl", dataModel); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/org/terasology/web/servlet/WsEventServlet.java
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,34 @@ | ||
/* | ||
* Copyright 2016 MovingBlocks | ||
* | ||
* 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 org.terasology.web.servlet; | ||
|
||
import org.eclipse.jetty.websocket.servlet.WebSocketServlet; | ||
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; | ||
|
||
/** | ||
* Registers the {@link EventSocket} class for all incoming connections. | ||
*/ | ||
public class WsEventServlet extends WebSocketServlet { | ||
|
||
private static final long serialVersionUID = -981505298711059433L; | ||
|
||
@Override | ||
public void configure(WebSocketServletFactory factory) { | ||
factory.getPolicy().setIdleTimeout(10000); // set a 10 second timeout | ||
factory.register(EventSocket.class); | ||
} | ||
} |
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,63 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<#include "metainfo.ftl"> | ||
|
||
<body> | ||
|
||
<div class="container"> | ||
|
||
<#assign tab = "log"> | ||
<#include "navigation.ftl"> | ||
|
||
<h3>Log output</h3> | ||
|
||
|
||
|
||
|
||
|
||
<div class="form-group"> | ||
<label for="comment">Comment:</label> | ||
<textarea class="form-control" rows="5" id="comment"></textarea> | ||
</div> | ||
|
||
|
||
<div id="sse"> | ||
<a href="javascript:WebSocketTest()">Run WebSocket</a> | ||
</div> | ||
|
||
|
||
<#include "footer.ftl"> | ||
|
||
</div> <!-- /container --> | ||
|
||
</body> | ||
|
||
<script type="text/javascript"> | ||
function WebSocketTest() | ||
{ | ||
if ("WebSocket" in window) { | ||
var ws = new WebSocket("ws://localhost:8080/events"); | ||
var textArea = document.getElementById('comment'); | ||
ws.onopen = function() { | ||
ws.send("HelloWorld"); | ||
textArea.value += 'Connected..\n'; | ||
}; | ||
ws.onmessage = function(evt) { | ||
textArea.value += evt.data; | ||
}; | ||
ws.onclose = function() { | ||
textArea.value += 'Closed\n'; | ||
}; | ||
} | ||
else { | ||
// The browser doesn't support WebSocket | ||
alert("WebSocket NOT supported by your Browser!"); | ||
} | ||
} | ||
</script> | ||
|
||
</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