Java实现websocket与微信小程序连接

微信的WebSocket接口和HTML5的WebSocket基本一样,是HTTP协议升级来的,做为一个新的Socket在B/S上使用,它实现了浏览器与服务器全双工通信。

在WebSocket出来之前,实现即时通讯通常使用Ajax来实现,而Ajax是通过轮询的方式进行实时数据的获取,轮询就是在指定的时间间隔内,进行HTTP 请求来获取数据,而这种方式会产生一些弊端,一方面产生过多的HTTP请求,占用带宽,增大服务器的相应,浪费资源,另一方面,因为不是每一次请求都会有数据变化(就像聊天室),所以就会造成请求的利用率低。
 

WebSocket正好能够解决上面的弊端,WebSocket是客户端与服务器之前专门建立一条通道,请求也只请求一次,而且可以从同道中实时的获取服务器的数据,所以当应用到实时的应用上时,WebSocket是一个很不错的选择。

WebSocket的链接不是以httphttps开头的,而是以wswss开头的,这里需要注意一下。

import com.ds.tech.service.SocketChatService;
import com.ds.tech.utility.log4j.LogWriter;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.server.standard.SpringConfigurator;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;


//websocket连接URL地址和可被调用配置
@ServerEndpoint(value="/wx/{modular}/{interfaces}",configurator = SpringConfigurator.class)
public class WebSocketChatCotroller {

    private static Logger logger = Logger.getRootLogger();

    //需要session来对用户发送数据, 获取连接特征userId
    private Session session;
    private String api;


    @Autowired
    private SocketChatService service;

    /**
     * @Title: onOpen
     * @Description: websocekt连接建立时的操作
     * @param @param userId 用户id
     * @param @param session websocket连接的session属性
     * @param @throws IOException
     */
    @OnOpen
    public void onOpen(@PathParam("modular") String modular,@PathParam("interfaces") String interfaces, Session session) throws IOException{
        this.session = session;
        this.api = "wx/"+modular+"/"+interfaces;
    }

    /**
     * @Title: onClose
     * @Description: 连接关闭的操作
     */
    @OnClose
    public void onClose(){
        logger.info(api+"链接关闭");
        this.session = null;
    }

    /**
     * @Title: onMessage
     * @Description: 收到消息后的操作
     * @param @param message 收到的消息
     * @param @param session 该连接的session属性
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        logger.info("接口:"+api+",入参:"+message);
        if(session ==null){
            logger.info("session null");
        }
        String apiResultString = null;
        try {
            apiResultString = service.getApi(api,message);
        }catch (Exception e){
            LogWriter.writeErrorLog("请求失败,原因:", e);
        }
        //向客户端发送消息发送
        sendMessageToUser(this.api,apiResultString);
    }

    /**
     * @Title: onError
     * @Description: 连接发生错误时候的操作
     * @param @param session 该连接的session
     * @param @param error 发生的错误
     */
    @OnError
    public void onError(Session session, Throwable error){
        logger.info("接口:"+api+"连接时发送错误:");
        error.printStackTrace();
    }

    /**
     * @Title: sendMessageToUser
     * @Description: 发送消息给用户下的所有终端
     * @param @param userId 用户id
     * @param @param message 发送的消息
     * @param @return 发送成功返回true,反则返回false
     */
    public Boolean sendMessageToUser(String api,String message){
            logger.info(api+"返回参数:"+message);
            try {
                this.session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
                logger.info(api+"返回参数失败:"+e);
                return false;
            }
            return true;
    }

}

import com.alibaba.fastjson.JSONObject;
import com.ds.tech.service.base.BaseService;
import com.ds.tech.utility.common.ConfigUtil;
import com.ds.tech.utility.http.HttpRequestUtil;
import com.ds.tech.utility.model.InputObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URLEncoder;

@Service
@Scope("prototype")
public class SocketChatService extends BaseService {
    public String getApi(String url,String message) throws IOException {
        InputObject inputObject = JSONObject.parseObject(message,InputObject.class);
        //请求参数
        String data =  inputObject.getData();
        apiParamMap.put("partner", inputObject.getPartner());
        apiParamMap.put("data", URLEncoder.encode(data, "utf-8"));
        apiParamMap.put("sign", inputObject.getSign());
        logger.info("小程序请求API系统-"+url+":"+apiParamMap);
        // 请求api
        return apiResultString = HttpRequestUtil.get(ConfigUtil.getSettings("api_host")+ url+ ".do", apiParamMap);

    }

}
 

猜你喜欢

转载自my.oschina.net/u/2611678/blog/2250442