In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
这几天在做web端实时展示服务端日志文件新增内容的功能。要满足实时的需求,我选择的方案是在web端跟服务端建立一个websocket链接,由服务端通过tail -f 命令将文件新增内容发送给web端。
关于websocket的介绍,可以参考这篇博文:http://www.cnblogs.com/lizhenghn/p/5155933.html(链接仅用于学习交流,如有版权问题请及时告知)。这里我主要想介绍的是在spring-boot框架下如何发布websocket服务。
一、在服务端发布websocket服务
服务端发布websocket服务有几种方式,包括Servlet容器扫描初始化、Spring扫描初始化。我使用的是第二种方式,可以将websocket服务定义为一个单独的类实例。
Spring扫描初始化时,需要先定义一个Bean:ServerEndpointExporter,以声明服务端。我把这个Bean独立放到一个websocket 配置类中。
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.socket.server.standard.ServerEndpointExporter;@Configurationpublic class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter (){ return new ServerEndpointExporter(); } }
接下来是定义websocket服务接口,并使用关键字 @ServerEndpoint("/websocketPath") 声明一个接口的访问路径。
import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.concurrent.CopyOnWriteArraySet;import javax.websocket.OnClose;import javax.websocket.OnError;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.RestController;@ServerEndpoint("/logWebsocket") @Component @RestControllerpublic class LogWebSocketHandle { private Session session;//每个websocket会话连接对应一个session private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet(); /** * 新的WebSocket请求开启。 * 新建立连接后会触发onOpen方法 * @throws JSchException * @throws IOException */ @OnOpen public void onOpen(Session session) throws JSchException, IOException { this.session = session; webSocketSet.add(this); //服务端保留session信息,并返回结果给客户端 //这个语句用于服务端给客户端发送数据 session.getBasicRemote().sendText("正在获取日志
"); } /** * WebSocket请求关闭。 * websocket连接关闭后,会触发onClose方法 */ @OnClose public void onClose() { webSocketSet.remove(this); } @OnError public void onError(Throwable thr) { thr.printStackTrace(); } /** * 客户端发送消息时,服务端通过onMessage方法接收 */ @OnMessage public void onMessage (String message, Session session) throws IOException, JSchException, InterruptedException { LOG.info("来自客户端的message:" + message); try { //process message //TODO } catch (IOException e) { e.printStackTrace(); }// 给客户端群发消息 // for ( Session item : webSocketSet ){ // item.getBasicRemote().sendText(message); // } } }
二、web端建立websocket连接
var websocket_host = _window.location.host;//与服务端建立websocket连接var websocket = new WebSocket("ws://"+websocket_host+"/项目名/logWebsocket");//连接建立后,会触发onopen方法websocket.onopen = function(event){ console.log("opened!"); $("#chart_multiple div").append(event.data); //向服务端发送数据 websocket.send(message);};//接收服务端的数据websocket.onmessage = function(event){ $("#chart_multiple div").append(event.data); $("#chart_multiple").scrollTop( $("#chart_multiple div").height()-$("#chart_multiple").height() );}
三、使用nginx转发时的额外配置
如果项目使用了nginx进行负载均衡,那么需要在nginx.conf配置文件中添加一个websocket转发配置,具体为:
location /#{websocket所在的项目名}/logWebsocket { proxy_pass http://ip:port/#{websocket所在的项目名}/logWebsocket; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 7200s;}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.