2018-12-21

BsetHttp分包发送

using BestHTTP;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using BestHTTP.WebSocket;

public class TestHttp : MonoBehaviour {
    WebSocket webSocket = null;
    public Texture2D tex;
   private byte[] bysts=new byte[] { };
    private byte[] bsc;
  
    // Use this for initialization
    IEnumerator Start () {
        //http://192.168.51.124:8001/txt/zhibiao.txt //http://192.168.51.124:8001/pic/n.jpg
        using (WWW www=new WWW("http://192.168.51.124:8001/pic/n.jpg"))
        {
            yield return www;
            byte[] bs = www.bytes;
            List bt = new List();
            bt.AddRange(BitConverter.GetBytes(bs.Length));
            bt.AddRange(bs);
            bsc = bt.ToArray();           
            Debug.Log(bsc.Length);
        }
        Init("");
    }
    void EnterByteQueue(byte[] bs)
    {
        List list = new List();
        list.AddRange(bysts);
        list.AddRange(bs);
        bysts = list.ToArray();
    }
    private void OnRequestFinish(HTTPRequest originalRequest, HTTPResponse response)
    {
       
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.A))
        {
            EnterByteQueue(bsc);
            Debug.Log("数据缓存到数组里");
        }     
        if (webSocket!=null&&!webSocket.IsOpen)
        {
            return;
        }
            if (bysts!=null&& bysts.Length>0)
            {
                byte[] bs = null;
                int leng = 0;
                leng = bysts.Length >= 1024 ? 1024 : bysts.Length;
                bs = new byte[leng];
                Debug.Log("send length:" + leng+"_:");
                Buffer.BlockCopy(bysts, 0, bs, 0, leng);
                int bystsL = bysts.Length - leng;
                byte[] bytes = new byte[bystsL];
                Buffer.BlockCopy(bysts, leng, bytes, 0, bystsL);
                bysts = bytes;
                SendBytes(bs);
            }
    }

    public bool Init(string strAddress)
    {
        
        if (webSocket == null)
        {

            //if (!strAddress.Contains("ws://"))
            //{
            //    Debug.Log("非法地址: " + strAddress);
            //    return false;
            //}
            strAddress = "ws://192.168.51.20:8889/websocket";
           // strAddress = "ws://10.101.101.14:81/hg-video/realTimeMsg?accessToken=0928cd3a-adb7-470c-8ae0-ba0dbad92d6b";
            // Create the WebSocket instance
            webSocket = new WebSocket(new Uri(strAddress));
            Debug.LogWarning("WebSocket Init****");
#if !BESTHTTP_DISABLE_PROXY && !UNITY_WEBGL
            if (HTTPManager.Proxy != null)
                webSocket.InternalRequest.Proxy = new HTTPProxy(HTTPManager.Proxy.Address, HTTPManager.Proxy.Credentials, false);
#endif

            // Subscribe to the WS events
            webSocket.OnOpen += OnOpen;
            webSocket.OnMessage += OnMessageReceived;
            webSocket.OnClosed += OnClosed;
            webSocket.OnError += OnError;
            webSocket.OnBinary += OnBinaryReceived;
            webSocket.Open();
            Debug.Log("webSocket isOpen" + webSocket.IsOpen);
        }
        return true;
    }
    public void SendBytes(byte[] buffer)
    {
        if (webSocket != null)
        {
            webSocket.Send(buffer);
            Debug.LogWarning("buffer.Length = " + buffer.Length);
        }
        else
        {
            Debug.LogWarning("jwebSocket == null ");
        }

    }
    private void OnBinaryReceived(WebSocket webSocket, byte[] data)
    {
       
    }

    private void OnError(WebSocket webSocket, Exception ex)
    {
      
    }

    private void OnClosed(WebSocket webSocket, ushort code, string message)
    {
        Debug.Log("OnClosed:"+code+"_MESSAGE:"+message);
    }

    private void OnMessageReceived(WebSocket webSocket, string message)
    {
        Debug.Log("OnMessageReceived:"+message);
    }

    private void OnOpen(WebSocket webSocket)
    {
        Debug.Log("OnOpen");
    }
    private void OnDestroy()
    {
        webSocket.Close();
    }
}

你可能感兴趣的:(2018-12-21)