Search Unity

Android and c# WebSockets

Discussion in 'Multiplayer' started by kandibobrik, Sep 29, 2017.

  1. kandibobrik

    kandibobrik

    Joined:
    Oct 21, 2016
    Posts:
    1
    hello!
    We make game and have problems with socket data transfer on Android.
    We can connect use socket to server but when we send any data to server (using method SendData()) there is not happen anything (I mean no errors, or receiving data on server), but this code work fine on pc in editor.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Net.Sockets;
    5. using System.Net;
    6. using System;
    7. using System.IO;
    8. using System.Text;
    9.  
    10. public class ServerConnector
    11. {
    12.     const int READ_BUFFER_SIZE = 255;
    13.  
    14.     private TcpClient client;
    15.     private byte[] readBuffer = new byte[READ_BUFFER_SIZE];
    16.     public string strMessage=string.Empty;
    17.     public string res=String.Empty;
    18.     private string pUserName;
    19.  
    20.     public ServerConnector()
    21.     {
    22.         m_state = ServerState.Connect;
    23.     }
    24.  
    25.     private ServerState m_state;
    26.  
    27.     //Server StartUp
    28.     public string fnConnectResult(string sNetIP, int iPORT_NUM,string sUserName)
    29.     {
    30.         try
    31.         {
    32.             Debug.Log("StartFnConnect");
    33.          
    34.             pUserName =sUserName;
    35.             // The TcpClient is a subclass of Socket, providing higher level
    36.             // functionality like streaming.
    37.             client = new TcpClient(sNetIP, iPORT_NUM);
    38.             // Start an asynchronous read invoking DoRead to avoid lagging the user
    39.             // interface.
    40.             Debug.Log("BeginRead");
    41.             client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(DoRead), null);
    42.             // Make sure the window is showing before popping up connection dialog.
    43.             Debug.Log("Connection Succeeded");
    44.             //    AttemptLogin(sUserName);
    45.             m_state = ServerState.Initialization;
    46.             return "Connection Succeeded";
    47.         }
    48.         catch(Exception ex)
    49.         {
    50.             Debug.Log (ex.Message);
    51.  
    52.             return "Server is not active.  Please start server and try again.      " + ex.ToString();
    53.         }
    54.     }
    55.    
    56.     //Server Callback
    57.     private void DoRead(IAsyncResult ar)
    58.     {
    59.         int BytesRead;
    60.         try
    61.         {
    62.             // Finish asynchronous read into readBuffer and return number of bytes read.
    63.             BytesRead = client.GetStream().EndRead(ar);
    64.             if (BytesRead < 1)
    65.             {
    66.                 // if no bytes were read server has close.
    67.                 Debug.Log("no bytes were read server has close.");
    68.                 return;
    69.             }
    70.             // Convert the byte array the message was saved into, minus two for the
    71.             // Chr(13) and Chr(10)
    72.             strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2);
    73.             Debug.Log(strMessage);
    74.             ProcessCommands(strMessage);
    75.             // Start a new asynchronous read into readBuffer.
    76.             client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(DoRead), null);
    77.  
    78.         }
    79.         catch
    80.         {
    81.             Debug.Log("Disconnected");
    82.         }
    83.     }
    84.  
    85.     public void SendPowerUp()
    86.     {
    87.         //SendData("PowerUp|");
    88.     }
    89.     public void SendAiming(ThrowData aiming)
    90.     {
    91.         SendData(LitJson.JsonMapper.ToJson(aiming));
    92.         //SendData("PowerUp|");
    93.     }
    94.    
    95.     public void StartGame()
    96.     {
    97.         int id = UnityEngine.Random.Range(0, 2);
    98.         CurrentGameData.PlayerID = id;
    99.         CurrentGameData.PlayerNumber = id;
    100.        
    101.         if(id == 1) id = 0;
    102.         else id = 1;
    103.         JsonGameData data = new JsonGameData(id, id);
    104.        
    105.         SendData(LitJson.JsonMapper.ToJson(data));
    106.     }
    107.  
    108.     // Process the command received from the server, and take appropriate action.
    109.     private void ProcessCommands(string data)
    110.     {
    111.         switch (m_state) {
    112.         case ServerState.Connect:
    113.             Debug.Log("Connect State" + data);
    114.             break;
    115.         case ServerState.Initialization:
    116.             Debug.Log("Initialization State" + data);
    117.         //    ServerActions.ParseJsonToInitData (data);
    118.             m_state = ServerState.GameMode;
    119.             break;
    120.         case ServerState.GameMode:
    121.             Debug.Log("GameMode State" + data);
    122.             ServerActions.ParseJsonToThrowDataData(data);
    123.             break;
    124.         case ServerState.Disconnect:
    125.             Debug.Log("Disconnect State" + data);
    126.             break;
    127.         default:
    128.             break;
    129.         }  
    130.     }
    131.  
    132.     // Use a StreamWriter to send a message to server.
    133.     public void SendData(string data)
    134.     {
    135.        
    136.         StreamWriter writer = new StreamWriter(client.GetStream());
    137.         writer.AutoFlush = true;
    138.         writer.WriteLine (string.Format("{0}",data));
    139.         writer.Flush();
    140.        
    141.     }
    142.     public void CloseSocket(){
    143.         client.Close ();
    144.     }
    145.        
    146. }
    147.  
    148.