forked from kylinsoong/wildfly-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
eac6fa9
commit f739545
Showing
5 changed files
with
81 additions
and
9 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
40 changes: 40 additions & 0 deletions
40
.../quickstart/src/main/java/org/wildfly/undertow/quickstart/websockets/WebSocketServer.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,40 @@ | ||
package org.wildfly.undertow.quickstart.websockets; | ||
|
||
import java.io.IOException; | ||
|
||
import io.undertow.Handlers; | ||
import io.undertow.Undertow; | ||
import io.undertow.server.handlers.resource.ClassPathResourceManager; | ||
import io.undertow.websockets.WebSocketConnectionCallback; | ||
import io.undertow.websockets.core.AbstractReceiveListener; | ||
import io.undertow.websockets.core.BufferedTextMessage; | ||
import io.undertow.websockets.core.WebSocketChannel; | ||
import io.undertow.websockets.core.WebSockets; | ||
import io.undertow.websockets.spi.WebSocketHttpExchange; | ||
|
||
public class WebSocketServer { | ||
|
||
public static void main(String[] args) { | ||
Undertow server = Undertow.builder().addHttpListener(8080, "localhost") | ||
.setHandler(Handlers.path().addPrefixPath("/myapp", Handlers.websocket(new WebSocketConnectionCallback(){ | ||
|
||
@Override | ||
public void onConnect(WebSocketHttpExchange exchange,WebSocketChannel channel) { | ||
|
||
channel.getReceiveSetter().set(new AbstractReceiveListener(){ | ||
|
||
@Override | ||
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException { | ||
WebSockets.sendText(message.getData(), channel, null); | ||
}}); | ||
|
||
channel.resumeReceives(); | ||
} | ||
})) | ||
.addPrefixPath("/", Handlers.resource(new ClassPathResourceManager(WebSocketServer.class.getClassLoader(),WebSocketServer.class.getPackage())).addWelcomeFiles("index.html"))) | ||
.build(); | ||
|
||
server.start(); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
undertow/quickstart/src/main/java/org/wildfly/undertow/quickstart/websockets/index.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,37 @@ | ||
<html> | ||
<head><title>Web Socket Test</title></head> | ||
<body> | ||
<script> | ||
var socket; | ||
if (window.WebSocket) { | ||
socket = new WebSocket("ws://localhost:8080/myapp"); | ||
socket.onmessage = function(event) { | ||
alert("Received data from websocket: " + event.data); | ||
}; | ||
socket.onopen = function(event) { | ||
alert("Web Socket opened!"); | ||
}; | ||
socket.onclose = function(event) { | ||
alert("Web Socket closed."); | ||
}; | ||
} else { | ||
alert("Your browser does not support Websockets. (Use Chrome)"); | ||
} | ||
|
||
function send(message) { | ||
if (!window.WebSocket) { | ||
return; | ||
} | ||
if (socket.readyState == WebSocket.OPEN) { | ||
socket.send(message); | ||
} else { | ||
alert("The socket is not open."); | ||
} | ||
} | ||
</script> | ||
<form onsubmit="return false;"> | ||
<input type="text" name="message" value="Hello, World!"/> | ||
<input type="button" value="Send Web Socket Data" onclick="send(this.form.message.value)"/> | ||
</form> | ||
</body> | ||
</html> |