Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

okhttp 链接websocket 报错

报错如下

已经连接上流
MESSAGE: [size=184 hex=08011094f598a8c2ffcca76e18b8452008320270623a036d7367429b011225313631303336303037303332305f36393136343433383236383434333631373938…]
已经连接上流
MESSAGE: [size=7982 hex=080210c68a9da8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742fa3d0ad03c0a1957656263617374…]
已经连接上流
MESSAGE: [size=2214 hex=080310d2bea1a8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742f2100ac80f0a1457656263617374…]
已经连接上流
MESSAGE: [size=7984 hex=080410bbdaa5a8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742fc3d0ad23c0a1957656263617374…]
已经连接上流
MESSAGE: [size=219 hex=08051090f2a9a8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a701122531363130333630303734…]
已经连接上流
MESSAGE: [size=13330 hex=080610d788aea8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742de670aed3c0a1957656263617374…]
已经连接上流
MESSAGE: [size=219 hex=080710cca6aea8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a701122531363130333630303735…]
已经连接上流
MESSAGE: [size=14126 hex=0808108ec4b2a8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742fa6d0ada2f0a1457656263617374…]
已经连接上流
MESSAGE: [size=219 hex=080910ccfabaa8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a701122531363130333630303738…]
已经连接上流
MESSAGE: [size=7996 hex=080a10d797bfa8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742883e0add3c0a1957656263617374…]
已经连接上流
MESSAGE: [size=6766 hex=080b10e7cdc3a8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742ba340a8f330a1457656263617374…]
已经连接上流
MESSAGE: [size=8012 hex=080c1092ebc7a8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742983e0aed3c0a1957656263617374…]
已经连接上流
MESSAGE: [size=7966 hex=080d10b9a2cca8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742ea3d0abf3c0a1957656263617374…]
已经连接上流
MESSAGE: [size=220 hex=080e10d7bfcca8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a801122531363130333630303833…]
已经连接上流
MESSAGE: [size=7959 hex=080f10afb1dda8c2ffcca76e18b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742e33d0ab83c0a1957656263617374…]
java.io.EOFException
    at okio.RealBufferedSource.require(RealBufferedSource.kt:199)
    at okio.RealBufferedSource.readByte(RealBufferedSource.kt:209)
    at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.java:117)
    at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:101)
    at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:273)
    at okhttp3.internal.ws.RealWebSocket$2.onResponse(RealWebSocket.java:213)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)`

代码如下

package utils;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import model.UrlAndParams;
import model.UserInfo;
import okhttp3.*;
import okio.ByteString;
import utils.Functions;
import model.UrlAndParams;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LiveWebSocket extends WebSocketListener {
    private WebSocket websocket=null;
    private JSONObject jsonobject=null;

    private static LiveWebSocket livewebsocket=null;


    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        websocket=webSocket;
    }

    @Override
    public void onMessage(WebSocket webSocket, String text) {

        try {
            jsonobject = new JSONObject(Boolean.parseBoolean(text));
            System.out.println(jsonobject);
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
    @Override
    public void onMessage(WebSocket webSocket, ByteString bytes) {

        System.out.println("已经连接上流");
        System.out.println("MESSAGE: " + bytes.toString());
    }

    @Override
    public void onClosing(WebSocket webSocket, int code, String reason) {
        webSocket.close(1000, null);
        livewebsocket=null;

    }
    @Override
    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
        t.printStackTrace();
    }
    /**
     * 初始化WebSocket服务器
     *
     */
    private static String byteToString(byte[] bytes) {
        if (null == bytes || bytes.length == 0) {
            return "";
        }
        String strContent = "";
        try {
            strContent = new String(bytes, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return strContent;
    }

    private void run() {
        String room_id="6916411971621686016";
        UserInfo userInfo = UserInfo.getSomeone();
        JSONObject object=Functions.webCastFetch(userInfo, room_id);
        String push_server=object.getString("push_server");
        String pattern1="wss://(.*?)/webcast/im/push/";
        Pattern r1 = Pattern.compile(pattern1);
        Matcher m1 = r1.matcher(push_server);
        String host=null;
        if(m1.find()) {
            System.out.println(m1.group(1));
            host=m1.group(1);
        }
        String imrpp=object.getString("imprp");
        String  cursor=object.getString("cursor");
//        String internal_ext=object.getString("internal_ext");
        System.out.println(push_server);
        System.out.println(cursor);
        System.out.println(imrpp);
        String url=UrlAndParams.getUrl_webCastWss(userInfo,push_server,imrpp,cursor,room_id);
        System.out.println(url);
        OkHttpClient client = new OkHttpClient.Builder().readTimeout(0,  TimeUnit.MILLISECONDS).build();
        Request.Builder builder = new Request.Builder();
        UrlAndParams.addWebsocketHeaders(builder,host);
        Request request=builder.url(url).build();
        client.newWebSocket(request, this);
        client.dispatcher().executorService().shutdown();
    }
    /**
     *
     * @param s
     * @return
     */
    public boolean sendMessage(String s){
        return websocket.send(s);
    }

    public void closeWebSocket(){
        livewebsocket=null;
        websocket.close(1000,"主动关闭");

    }
    /**
     * 获取全局的LiveWebSocket类
     * @return LiveWebSocket
     */
    public  LiveWebSocket getChartWebSocket(){
        if(livewebsocket==null) {
            livewebsocket =new LiveWebSocket();
            livewebsocket.run();
        }
        return livewebsocket;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...