Try WebSocket communication with jooby.
--jooby's QUICK START has ended
Place ws.html directly under the public directory.
ws.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocket sample</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(function(){
var ws = new WebSocket("ws://localhost:8080/ws");
ws.onmessage = function(message){
$("#log").prepend(message.data + "<br>");
}
$("#message").keyup(function(e){
ws.send($(this).val());
});
})
</script>
</head>
<body><input type="text" id="message"><div id="log"></div></body>
</html>
It is a simple configuration that receives a message, returns it to the sender, and broadcasts it.
App.java
{
assets("/ws.html", "ws.html");
ws("/ws", ws -> {
ws.onMessage(msg -> {
ws.send("send : " + msg.value());
ws.broadcast("broadcast : " + msg.value());
});
});
}
In each tab, both the message I sent and the broadcast are displayed, so it seems to be working properly.
I think that it is very easy and easy to understand because simple ** WebSocket ** communication is possible with only this amount of description. However, if you are familiar with Java for a long time, it doesn't look like Java code.
Recommended Posts