1. 程式人生 > >java websocket demo

java websocket demo

通過spring boot實現websocket服務端

  1. maven 依賴
<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
         <version>${spring.boot.version}</version>
     </dependency>
    <dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>1.3.5.RELEASE</version> </dependency>
  1. java服務端:
    2.1. webdocket config
import org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter (){ return new ServerEndpointExporter(); } }

2.2. websocket end point


import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.stereotype.Component;

@ServerEndpoint("/ws")
@Component
public class WsServerController {

    private Session session;

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        System.out.println("on open");
    }

    @OnClose
    public void onClose() {
        System.out.println("on close");
    }

    @OnMessage
    public void onMessage(String msg, Session session) {
        System.out.println("from client msg: " + msg);
        this.sendMsg("from server");
    }

    public void sendMsg(String msg) {
        try {
            this.session.getBasicRemote().sendText(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.3. 啟動伺服器

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication action = new SpringApplication(Application.class, "");
        action.run(args);
    }
}
  1. js 客戶端
(function(win, $) {
    function WSClient() {
        if ("WebSocket" in win) {
            console.log('瀏覽器支援WebSocket');
        } else {
            console.log('瀏覽器不支援WebSocket');
        }
    }

    WSClient.prototype.init = function() {
        var url = "ws://localhost:8080/ws";
        this.ws = new WebSocket(url);

        this.ws.onopen = function() {
            console.log('onopen');
            this.ws.send('hello wolrd!!');
        }.bind(this);

        this.ws.onmessage = function(msg) {
            console.log('onmessage');
            console.log(msg.data);
        }.bind(this);

        this.ws.onclose = function() {
            console.log('onclose');
        }.bind(this);

        this.ws.onerror = function() {
            console.log(arguments);
            console.log('onerror');
        }.bind(this);
    }

    WSClient.prototype.sendMsg = function(msg) {
        this.ws.send(msg);
    }

    $(function() {
        var ws = new WSClient();
        ws.init();

    })

})(window, jQuery)
  1. 以上實現了基本的js和java後端通訊功能