通过websocket实现顾客在小程序下单点餐,web前端自动提示“您有新的订单”

html5自带了websocket,实现第一步,在pom.xml中引入websocket的依赖,我这里用的是springBoot。
第一步

 
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-websocketartifactId>
        dependency>

第二步
使用html5自带的websocket与后台建立连接

<script>
    var websocket = null;
    if('WebSocket' in window) {
     
        websocket = new WebSocket('ws://localhost/webSocket');  //这里写你的接口,协议用ws
    }else {
     
        alert('该浏览器不支持websocket!');
    }

    websocket.onopen = function (event) {
     
        console.log('建立连接');
    }

    websocket.onclose = function (event) {
     
        console.log('连接关闭');
    }

    websocket.onmessage = function (event) {
     
        console.log('收到消息:' + event.data)
        //弹窗提醒, 播放音乐
        $('#myModal').modal('show');

        document.getElementById('notice').play();
    }

    websocket.onerror = function () {
     
        alert('websocket通信发生错误!');
    }

    window.onbeforeunload = function () {
     
        websocket.close();
    }

</script>

第三步
在java后台进行websocket的bean配置

@Component
public class WebSocketConfig {
     

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
     
        return new ServerEndpointExporter();
    }
}

第四步
websocket方法执行的固定套路

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;

@Component
@ServerEndpoint("/webSocket")
@Slf4j
public class WebSocket {
     

    private Session session;

    private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>();

    @OnOpen
    public void onOpen(Session session) {
     
        this.session = session;
        webSocketSet.add(this);
        log.info("【websocket消息】有新的连接, 总数:{}", webSocketSet.size());
    }

    @OnClose
    public void onClose() {
     
        webSocketSet.remove(this);
        log.info("【websocket消息】连接断开, 总数:{}", webSocketSet.size());
    }

    @OnMessage
    public void onMessage(String message) {
     
        log.info("【websocket消息】收到客户端发来的消息:{}", message);
    }

    public void sendMessage(String message) {
     
        for (WebSocket webSocket: webSocketSet) {
     
            log.info("【websocket消息】广播消息, message={}", message);
            try {
     
                webSocket.session.getBasicRemote().sendText(message);
            } catch (Exception e) {
     
                e.printStackTrace();
            }
        }
    }

}

这个时候前后端的websocket已经成功建立连接,web端可以写一个弹窗做提示。

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<#--弹窗-->
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">
                    提醒
                </h4>
            </div>
            <div class="modal-body">
                你有新的订单
            </div>
            <div class="modal-footer">
                <button onclick="javascript:document.getElementById('notice').pause()" type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button onclick="location.reload()" type="button" class="btn btn-primary">查看新的订单</button>
            </div>
        </div>
    </div>
</div>

<#--播放音乐-->
<audio id="notice" loop="loop">
    <source src="你的提示音地址" type="audio/mpeg" />
</audio>

按照上诉套路就能实现移动端发送下单,后台接收到订单请求后刷新前端页面的效果。

你可能感兴趣的:(java,微信小程序)