webSocket 案例 使用spring4.0.0

因爲最近一個項目中要使用常練接的問題 要求是使用spring中的websocket 但是 親們 你們知道嗎? spring中websocket是一個深坑呀~ 太坑了
所以整理了一下 關於spring-websocket的相關問題 防止以後忘記了~~~~~~~ 啊哈哈 
步驟:
        1、將websocket-config注入到spring中
        2、將websocket配置的鏈接注入到websocket-config中
        3、編寫相關的業務邏輯

WebSocket-Configer
package com.keepalive.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServerEndpointRegistration;

import com.gateguard.keepalive.service.CaseEndPointService;

public class WebSocketConfiger
{
    @Bean
    public ServerEndpointExporter endpointExporter(){
        return new ServerEndpointExporter();
    }

    /**
     * 綁定webSocket的鏈接和實現類
     * @return
     */
    @Bean
    public ServerEndpointRegistration caseCount()
    {
        System.out.println("caseCount");
        return new ServerEndpointRegistration("/casecount", CaseCountEndPoint.class);
    }

    /**
     * 綁定webSocket的鏈接 主要是單例模式 
     * @return
     */
    @Bean
    public ServerEndpointRegistration caseCountSingle()
    {
        System.out.println("caseCountSingle");
        return new ServerEndpointRegistration("/casecountsingle", new CaseCountEndPoint(caseEndPointService()));
    }

    @Bean
    public CaseEndPointService caseEndPointService() {
        return new CaseEndPointService();
    }
}

spring-configer
<beans:bean id="websocket" class="com.keepalive.controller.WebSocketConfiger">
CaseCountEndPoint
package com.keepalive.controller;

import java.io.IOException;

import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

import org.springframework.beans.factory.annotation.Autowired;

import com.gateguard.keepalive.service.CaseEndPointService;

public class CaseCountEndPoint extends Endpoint 
{
    private final CaseEndPointService caseEndPointService;//這裏 final必須添加

    @Autowired
    public CaseCountEndPoint(CaseEndPointService caseEndPointService) {
        this.caseEndPointService = caseEndPointService;
    }

    @Override
    public void onOpen(final Session session, EndpointConfig endpointConfig) {
        System.out.println("進入 webSocket onOpen");
        session.addMessageHandler(new MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String message) {
                System.out.println("接受信息"+message);
                try {
                    session.getBasicRemote().sendText(String.valueOf(caseEndPointService.getCount()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
CaseEndPointService
package com.keepalive.service;

public class CaseEndPointService 
{
    public double getCount()
    {
       return Math.random();
    }
}



jsp:


使用lib文件:spring-websocket-4.0.0.RELEASE.jar

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章