关键词搜索

源码搜索 ×
×

用spring boot提供websocket后台

发布2019-04-09浏览583次

详情内容

websocket,提供客户端与服务器之间交流的双工协议(见拙作《WebSocket》),使用spring boot,可以很方便地提供websocket的服务器端。

主要是标上几个注解。

主体代码:mySocket.java

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

@ServerEndpoint("/ws")
@Component
public class CaptureSocket {
    private final static Logger LOGGER = LoggerFactory.getLogger(CaptureSocket.class);

    private static CopyOnWriteArraySet<CaptureSocket> set = new CopyOnWriteArraySet<>();
    private Session session;

    @OnOpen
    public void onOpen(Session session) throws IOException,InterruptedException {
        this.session=session;
        set.add(this);
        LOGGER.info("新连接来自:" + session.getRequestURI().getHost());
    }

    @OnClose
    public void onClose(){
        set.remove(this);
        LOGGER.info("连接关闭:" + this.session.getRequestURI().getHost());
    }

    @OnMessage
    public void onMessage(String message,Session session){
        LOGGER.info("来自客户端消息:" + message);
    }

    @OnError
    public void onError(Session session,Throwable err){
        LOGGER.info(err.getMessage());
        err.printStackTrace();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

配置文件:mySocketConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class CaptureSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这个配置文件无须显示调用,系统会自动解释。究其原因,应该是标注“@configuration”起了作用。

此配置文件的作用,从代码来看,是为了配合解释服务器端点用的?,主体代码mySocket.java里有个标注:@ServerEndpoint("/ws")

相应的客户端代码有:

var ws;
function wsconnect() {
	var ws = new WebSocket("ws://192.168.0.98:8085/ws");//最后一个ws应该就是EndPoint

	ws.onopen = function(evt) {
		console.log("Connection open ...");
		ws.send("hello socket!");
	};

	ws.onmessage = function(evt) {
		console.log("receive message: " + evt.data + ":" + (new Date()));
	};

	ws.onclose = function(evt) {
		console.log("Connection closed.Reconnect will be attempted in 1 second.", evt.reason);
		setTimeout(function() {
			ws = wsconnect();
		}, 1000);
	};

	ws.onerror = function(err) {
		console.error('Socket encountered error: ', err.message, 'Closing socket');
		ws.close();
	};

	return ws;
}

$(function(){
	ws = wsconnect();
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载