-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #402 from uezo/add-socket-client
Add client for socket communications
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using UnityEngine; | ||
|
||
namespace ChatdollKit.Network | ||
{ | ||
public class SocketClient : MonoBehaviour | ||
{ | ||
private TcpClient client; | ||
private NetworkStream stream; | ||
[SerializeField] | ||
private string address = "127.0.0.1"; | ||
public string Address { get { return address; } } | ||
[SerializeField] | ||
private int port = 0; | ||
public int Port { get { return port; } } | ||
|
||
public bool IsConnected { get { return client != null && client.Connected; }} | ||
|
||
public void Connect(string address = null, int port = 0) | ||
{ | ||
if (!string.IsNullOrEmpty(address)) | ||
{ | ||
this.address = address; | ||
} | ||
|
||
if (port > 0) | ||
{ | ||
this.port = port; | ||
} | ||
|
||
try | ||
{ | ||
client = new TcpClient(Address, Port); | ||
stream = client.GetStream(); | ||
Debug.Log($"Connected to server: {Address}:{Port}"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.LogError($"Failed to connect to server: {ex.Message}"); | ||
} | ||
} | ||
|
||
public void SendMessageToServer(string message) | ||
{ | ||
if (client == null || !client.Connected) | ||
{ | ||
Debug.LogError("Not connected to the server."); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
byte[] data = Encoding.UTF8.GetBytes(message + "\n"); | ||
stream.Write(data, 0, data.Length); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.LogError($"Failed to send message: {ex.Message}"); | ||
} | ||
} | ||
|
||
void OnApplicationQuit() | ||
{ | ||
Disconnect(); | ||
} | ||
|
||
public void Disconnect() | ||
{ | ||
if (stream != null) stream.Close(); | ||
if (client != null) client.Close(); | ||
Debug.Log("Disconnected from server."); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.