Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

okhttp 链接websocket 报错求解原因

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);
    }

    @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();
    }
    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="6916458961911614208";
        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");
        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();
        okhttp3.Request.Builder builder = new Request.Builder();
        UrlAndParams.addWebsocketHeaders(builder,host);
        Request request=builder.url(url).build();
        client.newWebSocket(request, this);
        client.dispatcher().executorService().shutdown();
    }

    public boolean sendMessage(String s){
        return websocket.send(s);
    }

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

    }
    public  LiveWebSocket getChartWebSocket(){
        if(livewebsocket==null) {
            livewebsocket =new LiveWebSocket();
            livewebsocket.run();
        }
        return livewebsocket;
    }

}
public static void addWebsocketHeaders(Request.Builder builder,String host)
    {
        builder
                .addHeader("Sec-Websocket-Protocol","pbbp")
                .addHeader("Upgrade","websocket")
                .addHeader("Connection","Upgrade")
                .addHeader("Sec-WebSocket-Key","J4axdrB5EVmab8YnJ4z3bw==")
                .addHeader("Sec-WebSocket-Version","13")
                .addHeader("Accept-Encoding","gzip")
                .addHeader("host",host)
                .addHeader("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 13_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Cronet Mobile/15E148 Safari/605.1");



    }

报错图下

已经连接上流
MESSAGE: [size=185 hex=080110a2c0dfabc4ffcd8cb90118b8452008320270623a036d7367429b011225313631303336383337353738355f363931363437393530353133373732333832…]
已经连接上流
MESSAGE: [size=1442 hex=080210eaace0abc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742ed0a0ac3090a14576562636173…]
已经连接上流
MESSAGE: [size=23838 hex=080310e3fce1abc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742e8b9010ab40c0a145765626361…]
已经连接上流
MESSAGE: [size=15734 hex=08041084cae7abc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742c17a0a97250a14576562636173…]
已经连接上流
MESSAGE: [size=23906 hex=080510949fe9abc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742acba010ac2460a195765626361…]
已经连接上流
MESSAGE: [size=14697 hex=08061093f5eeabc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742b4720ab92c0a14576562636173…]
已经连接上流
MESSAGE: [size=16563 hex=080710ccbef0abc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742fd80010ae2070a145765626361…]
已经连接上流
MESSAGE: [size=20877 hex=080810f78ff2abc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742d7a2010ade030a1a5765626361…]
已经连接上流
MESSAGE: [size=8183 hex=08091092a4f7abc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742c23f0ade030a1a576562636173…]
已经连接上流
MESSAGE: [size=31940 hex=080a10d58bf9abc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d7367428ef9010aa9460a195765626361…]
已经连接上流
MESSAGE: [size=221 hex=080b108cdffeabc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a8011225313631303336383338…]
已经连接上流
MESSAGE: [size=19959 hex=080c10e2b280acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742c19b010aa8450a145765626361…]
已经连接上流
MESSAGE: [size=25969 hex=080d10ba8082acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742bbca010aaa460a195765626361…]
已经连接上流
MESSAGE: [size=15392 hex=080e10dad087acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742eb770ab8070a14576562636173…]
已经连接上流
MESSAGE: [size=17240 hex=080f10a29a89acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a286010aad460a195765626361…]
已经连接上流
MESSAGE: [size=2169 hex=081010e2ea8eacc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742c4100a990f0a14576562636173…]
已经连接上流
MESSAGE: [size=6997 hex=0811108bb690acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a0360ae2070a14576562636173…]
已经连接上流
MESSAGE: [size=34893 hex=081210d28092acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d7367429790020abf460a195765626361…]
已经连接上流
MESSAGE: [size=38822 hex=081310c8d097acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742f0ae020aad5d0a145765626361…]
已经连接上流
MESSAGE: [size=31708 hex=0814109e9d99acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a6f7010a914c0a145765626361…]
已经连接上流
MESSAGE: [size=17387 hex=08151096f19eacc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742b587010ac2440a145765626361…]
已经连接上流
MESSAGE: [size=18210 hex=081610a1c1a0acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742ec8d010a97480a145765626361…]
已经连接上流
MESSAGE: [size=50703 hex=081710e4e5a7acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742d98b030aac460a195765626361…]
已经连接上流
MESSAGE: [size=27741 hex=081810de99a9acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742a7d8010ab4460a195765626361…]
已经连接上流
MESSAGE: [size=24352 hex=081910b9efaeacc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742eabd010aa11c0a125765626361…]
已经连接上流
MESSAGE: [size=11116 hex=081a1099c3b0acc4ffcd8cb90118b84520082a150a0d636f6d70726573735f7479706512046e6f6e65320270623a036d736742b7560ac60f0a14576562636173…]
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)

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...