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后端通信功能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值