Делаю серверный скрипт на Юнити
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class SocketMng : MonoBehaviour
{
private const int MAX_CONNECTION = 10;
private int port = 4040;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted = false;
private byte error;
private void Start()
{
try {
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
//hostId = NetworkTransport.AddHost(topo, port, null);
webHostId = NetworkTransport.AddWebsocketHost(topo, port, null);
isStarted = true;
Debug.Log("started server");
}
catch {
Debug.Log("unable to start server");
}
}
//start listening
private void Update()
{
// we don't want Update to run if Client is not started.
if (!isStarted) return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
//server can receive communications from many clients.
//every single client is going to have a different connection.
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer,
bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.Nothing: //1
break;
case NetworkEventType.ConnectEvent: //2
Debug.Log("Player cId: " + " has connected.");
break;
case NetworkEventType.DataEvent: //3
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Player cId: " + " has sent : " + msg);
break;
case NetworkEventType.DisconnectEvent: //4
Debug.Log("Player cId: " + " has disconnected.");
break;
}
}
}
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class SocketMng : MonoBehaviour
{
private const int MAX_CONNECTION = 10;
private int port = 4040;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted = false;
private byte error;
private void Start()
{
try {
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
//hostId = NetworkTransport.AddHost(topo, port, null);
webHostId = NetworkTransport.AddWebsocketHost(topo, port, null);
isStarted = true;
Debug.Log("started server");
}
catch {
Debug.Log("unable to start server");
}
}
//start listening
private void Update()
{
// we don't want Update to run if Client is not started.
if (!isStarted) return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
//server can receive communications from many clients.
//every single client is going to have a different connection.
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer,
bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.Nothing: //1
break;
case NetworkEventType.ConnectEvent: //2
Debug.Log("Player cId: " + " has connected.");
break;
case NetworkEventType.DataEvent: //3
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Player cId: " + " has sent : " + msg);
break;
case NetworkEventType.DisconnectEvent: //4
Debug.Log("Player cId: " + " has disconnected.");
break;
}
}
}
Далее у меня клиентское приложение на питоне, которое передает данные
Синтаксис:
Используется python
import socket
host = "127.0.0.1"
port = 4040
data = "1,2,3"
# SOCK_STREAM means TCP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to the server and send the data
sock.connect((host, port))
sock.sendall(data.encode("utf-8"))
print("response...")
response = sock.recv(1024).decode("utf-8")
print(response)
finally:
sock.close()
host = "127.0.0.1"
port = 4040
data = "1,2,3"
# SOCK_STREAM means TCP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to the server and send the data
sock.connect((host, port))
sock.sendall(data.encode("utf-8"))
print("response...")
response = sock.recv(1024).decode("utf-8")
print(response)
finally:
sock.close()
Подключение происходит, но получаю ответ:
response...
HTTP/1.0 403 Forbidden
server: libwebsockets
content-type: text/html
content-length: 38
Вопрос как подключиться к сетевоему движку юнити. Где посмотреть формат обмена сообщениями, чтобы клиент и сервер друг друга понимали?