Websocket

Jboot 内置了 Undertow 服务器,完美支持 websocket 功能, tomcat8 也是内置了对 websocket 的支持。

在使用 websocket 之前,需要添加如下配置:

  1. jboot.web.webSocketEndpoint=your-endpoint-class-name

例如:

  1. //多个 endpoint 用英文逗号(,) 隔开。
  2. jboot.web.webSocketEndpoint=io.jboot.test.websocket.WebsocketDemo

WebsocketDemo 的代码如下:

  1. package io.jboot.test.websocket;
  2. import javax.websocket.OnMessage;
  3. import javax.websocket.Session;
  4. import javax.websocket.server.ServerEndpoint;
  5. @ServerEndpoint("/myapp.ws")
  6. public class WebsocketDemo {
  7. @OnMessage
  8. public void message(String message, Session session) {
  9. for (Session s : session.getOpenSessions()) {
  10. System.out.println("receive : " + message);
  11. s.getAsyncRemote().sendText(message);
  12. }
  13. }
  14. }

这里要注意:@ServerEndpoint("/myapp.ws") 中的内容 /myapp.ws 必须有后缀,后缀可以自定义,否则会被 JFinal 的拦截。

我们还需要写一个 Controller,来渲染 html 页面。

  1. @RequestMapping("/websocket")
  2. public class WebsocketController extends JbootController {
  3. public void index() {
  4. render("/websocket.html");
  5. }
  6. }

websocket.html 的内容如下:

  1. <html>
  2. <head><title>Undertow Chat</title>
  3. <script>
  4. var socket;
  5. if (window.WebSocket) {
  6. // 这里的 /myapp.ws 必须和以上 @ServerEndpoint("/myapp.ws")
  7. // 中配置的内容保持一致
  8. socket = new WebSocket("ws://127.0.0.1:8080/myapp.ws");
  9. socket.onmessage = function (event) {
  10. var chat = document.getElementById('chat');
  11. chat.innerHTML = chat.innerHTML + event.data + "<br />";
  12. };
  13. } else {
  14. alert("Your browser does not support Websockets. (Use Chrome)");
  15. }
  16. function send(message) {
  17. if (!window.WebSocket) {
  18. return false;
  19. }
  20. if (socket.readyState == WebSocket.OPEN) {
  21. socket.send(message);
  22. } else {
  23. alert("The socket is not open.");
  24. }
  25. return false;
  26. }
  27. </script>
  28. <style type="text/css">
  29. html,body {width:100%;height:100%;}
  30. html,body,ul,ol,dl,li,dt,dd,p,blockquote,fieldset,legend,img,form,h1,h2,h3,h4,h5,h6 {margin:0;padding:0;}
  31. body {
  32. font:normal 12px/1.5 Arial,Helvetica,'Bitstream Vera Sans',sans-serif;
  33. background: #c5deea; /* Old browsers */
  34. background: -moz-linear-gradient(top, #c5deea 0%, #066dab 100%); /* FF3.6+ */
  35. background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #c5deea), color-stop(100%, #066dab)); /* Chrome,Safari4+ */
  36. background: -webkit-linear-gradient(top, #c5deea 0%, #066dab 100%); /* Chrome10+,Safari5.1+ */
  37. background: -o-linear-gradient(top, #c5deea 0%, #066dab 100%); /* Opera 11.10+ */
  38. background: -ms-linear-gradient(top, #c5deea 0%, #066dab 100%); /* IE10+ */
  39. background: linear-gradient(to bottom, #c5deea 0%, #066dab 100%); /* W3C */
  40. height: 90%;
  41. }
  42. .center {
  43. margin-left: auto;
  44. margin-right: auto;
  45. width: 70%;
  46. background: white;
  47. }
  48. .chatform {
  49. margin-left: auto;
  50. margin-right: auto;
  51. margin-bottom: 0;
  52. width: 70%;
  53. }
  54. form{
  55. width: 100%;
  56. }
  57. label{
  58. display: inline;
  59. width: 100px;
  60. }
  61. #msg{
  62. display: inline;
  63. width: 100%;
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <div class="page">
  69. <div class="center" >
  70. <h1>JSR-356 Web Socket Chat</h1>
  71. <div id="chat" style="height:100%;width: 100%; overflow: scroll;">
  72. </div>
  73. <form onsubmit="return false;" class="chatform" action="">
  74. <label for="msg">Message</label>
  75. <input type="text" name="message" id="msg"
  76. onkeypress="if(event.keyCode==13) { send(this.form.message.value); this.value='' } " />
  77. </form>
  78. </div>
  79. </div>
  80. </body>
  81. </html>