WebGL and UNet. Cant seem to get in proper contact with server from a browser - unity3d

I am trying to setup a multiplayer game to be played through a browser using UNet. In the editor everything works fine, I followed a tutorial (https://www.youtube.com/watch?v=qGkkaNkq8co) that said it should work in webgl but sadly it does not. There are two scripts as of yet one for the client and one for the server linked below. This is using mostly the LLAPI.
Whether I use the networkServer.usewebsocket = true or not, the result is the same.
The debug of "finished connection" does show so it completes the OnConnection function.
From the editor everything works fine but from the browser I get the following error:
"Attempt to send to not connected connection {1}" -- Since I get this error when ASKNAME is asked and when ASKPOSITION is asked I suspect that the SEND function is the problem im trying to send something the browser cant understand. But I don't know why.
private int port = 3001;
private List<ServerClient> clients = new List<ServerClient>();
private float lastMovementUpdate;
private float movementUpdateRate = 0.05f;
private int socketId;
private int webHostId;
private int reliableChannel;
private int unReliableChannel;
private bool isStarted = false;
private byte error;
private void Start()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unReliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_CONNECTION);
socketId = NetworkTransport.AddHost(topo, port, null);
webHostId = NetworkTransport.AddWebsocketHost(topo, port, null);
isStarted = true;
}
private void Update()
{
if (!isStarted)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
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 " + connectionId + "has connected");
OnConnection(connectionId);
break;
case NetworkEventType.DataEvent: //3
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving From " + connectionId + "has sent : " + msg);
string[] splitData = msg.Split('|');
switch (splitData[0])
{
case "NAMEIS":
OnNameIS(connectionId, splitData[1]);
break;
case "MYPOSITION":
OnMyPosition(connectionId, float.Parse(splitData[1]), float.Parse(splitData[2]));
break;
default:
Debug.Log("invalid message : " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent: //4
Debug.Log("Player " + connectionId + "has disconnected");
OnDiconnection(connectionId);
break;
}
if (clients.Count > 0)
{
if (Time.time - lastMovementUpdate > movementUpdateRate)
{
lastMovementUpdate = Time.time;
string posMsg = "ASKPOSITION|";
foreach (ServerClient sc in clients)
posMsg += sc.connectionId.ToString() + '%' + sc.position.x.ToString() + '%' + sc.position.y.ToString() + '|';
posMsg = posMsg.Trim('|');
Send(posMsg, unReliableChannel, clients);
}
}
}
private void OnConnection(int cnnId)
{
Debug.Log("Arrived at connection");
ServerClient c = new ServerClient();
c.connectionId = cnnId;
c.playerName = "TEMP";
clients.Add(c);
string msg = "ASKNAME|" + cnnId + "|";
foreach (ServerClient sc in clients)
msg += sc.playerName + '%' + sc.connectionId + '|';
msg = msg.Trim('|');
Send(msg, reliableChannel, cnnId);
Debug.Log("Finished at connection");
}
private void OnNameIS(int cnnId, string playerName)
{
// link the name to the connection ID
clients.Find(x => x.connectionId == cnnId).playerName = playerName;
// Tell evertone that a new player has connected
Send("CNN|" + playerName + '|' + cnnId, reliableChannel, clients);
}
private void Send(string message, int channelId, int cnnId)
{
List<ServerClient> c = new List<ServerClient>();
c.Add(clients.Find(x => x.connectionId == cnnId));
Send(message, channelId, c);
}
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(socketId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}
private void OnDiconnection(int cnnId)
{
clients.Remove(clients.Find(x => x.connectionId == cnnId));
string msg = "DC|" + cnnId;
Send(msg, reliableChannel, clients);
}
private void OnMyPosition(int cnnId, float x, float y)
{
clients.Find(clientInQuestion => clientInQuestion.connectionId == cnnId).position = new Vector3(x, y, 0);
}
And then the client:
private const int MAX_CONNECTION = 100;
private int port = 3001;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unReliableChannel;
private int connectionId;
private int clientId;
private bool isConnected = false;
public bool isStarted = false;
private float connectionTime;
private string playerName;
private byte error;
public GameObject playerPrefab;
public Dictionary<int, player> players = new Dictionary<int, player>();
public void Connect()
{
Debug.Log("connecting...");
// does player have a name?
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if (pName == "")
{
Debug.Log("Enter a name");
return;
}
playerName = pName;
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, 0);
connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
connectionTime = Time.time;
isConnected = true;
Debug.Log("connected");
}
private void Update()
{
connectionTime += Time.deltaTime;
if (!isConnected)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("receiving : " + msg);
string[] splitData = msg.Split('|');
switch (splitData[0])
{
case "ASKNAME":
OnAskName(splitData);
break;
case "CNN":
SpawnPlayer(splitData[1], int.Parse(splitData[2]));
break;
case "DC":
playerDisconnected(int.Parse(splitData[1]));
break;
case "ASKPOSITION":
OnAskPosition(splitData);
break;
default:
Debug.Log("invalid message : " + msg);
break;
}
break;
}
}
private void OnAskName(string[] data)
{
// set the client ID
clientId = int.Parse(data[1]);
// send our name to the server
Send("NAMEIS|" + playerName, reliableChannel);
// create all the other players
for (int i = 2; i < data.Length - 1; i++)
{
string[] d = data[i].Split('%');
SpawnPlayer(d[0], int.Parse(d[1]));
}
}
private void OnAskPosition(string[] data)
{
if (!isStarted)
return;
//update everyone else
for (int i = 1; i <= data.Length-1; i++)
{
string[] d = data[i].Split('%');
//prevent the server from updating us
if (clientId != int.Parse(d[0]))
{
Debug.Log("updating position");
Vector3 position = Vector3.zero;
position.x = float.Parse(d[1]);
position.y = float.Parse(d[2]);
players[int.Parse(d[0])].avatar.transform.position = position;
}
}
//send out own position
Vector3 myPosition = players[clientId].avatar.transform.position;
string m = "MYPOSITION|" + myPosition.x.ToString() + '|' + myPosition.y.ToString();
Send(m, unReliableChannel);
}
private void SpawnPlayer(string playerName, int cnnId)
{
GameObject go = Instantiate(playerPrefab) as GameObject;
// is this ours?
if(cnnId == clientId)
{
// add mobility
go.AddComponent<PlayerController>();
// remove Canvas
GameObject.Find("Canvas").SetActive(false);
isStarted = true;
}
player p = new player();
p.avatar = go;
p.playerName = playerName;
p.avatar.GetComponentInChildren<TextMesh>().text = playerName;
p.connectionId = cnnId;
players.Add(cnnId, p);
}
private void Send(string message, int channelId)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
NetworkTransport.Send(hostId, connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
private void playerDisconnected(int cnnId)
{
Destroy(players[cnnId].avatar);
Debug.Log("player : " + players[cnnId].playerName + " has disconnected");
players.Remove(cnnId);
}
}
Any feedback would be greatly appreciated, I have tried looking through the documentation but I feel I have done everything that it says to do in that (https://docs.unity3d.com/Manual/UNetUsingTransport.html) at the bottom it talks about webgl support, but it doesn't seem to have worked.

Fixed this issue,
Answer was that I was sending to HostID and not webSocketId
so this:
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(hostId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}
was changed to this:
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log("sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes(message);
foreach(ServerClient sc in c)
{
NetworkTransport.Send(webSocketId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}

Related

Multi-Client Chat and file transfer in linux

\I want to make a socket application that will run on Linux and provide multiple messaging and file transfer. Can you share sample code? Can you help me i am a second year computer engineering student?
I found a similar code in my research, but I cannot connect to my linux server from different computers.I learned this code from github #HondaPL.
This is client codes
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using MessengerLogic;
using System.IO;
namespace Messenger
{
public class Connection
{
public static int BufferSize = 1024;
public static string nick;
public static IPAddress ip = IPAddress.Parse("-.-.-.-");
public static int port = 1234;
public static TcpClient client = new TcpClient();
public static List<string> users = new List<string>();
public static bool isEnd = false;
public static string[] fromServer = new string[30];
public void Connect(string nickname)
{
nick = nickname;
client.Connect(ip, port);
nickname += "$Welcome$";
byte[] buffer = Encoding.ASCII.GetBytes(nickname);
NetworkStream ns = client.GetStream();
Console.WriteLine(nickname);
ns.Write(buffer, 0, buffer.Length);
Thread thread = new Thread(o => ReceiveData((TcpClient)o));
thread.Start(client);
}
static void ReceiveData(TcpClient client)
{
NetworkStream ns = client.GetStream();
byte[] receivedBytes = new byte[1024];
int byte_count;
StringParser parser = new StringParser();
while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0)
{
fromServer = new string[30];
users.Clear();
string message = Encoding.ASCII.GetString(receivedBytes, 0, byte_count);
fromServer = parser.Parser(message);
if (fromServer[0] == "List")
{
for (int i = 1; i < fromServer.Length; i++)
{
users.Add(fromServer[i]);
}
isEnd = true;
}
else
{
Console.WriteLine(message);
Console.WriteLine(fromServer[0]);
Console.WriteLine(fromServer[1]);
FrmOnlineUsers.frm.AssignValues(fromServer[0], fromServer[1]);
FrmOnlineUsers.frm.MessageFromClient();
}
}
}
public List<string> Refresh()
{
NetworkStream ns = client.GetStream();
string temp = nick;
temp += "$List$";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
while (!isEnd)
{
}
isEnd = false;
return users;
}
public void SendMessage(string who, string msg)
{
NetworkStream ns = client.GetStream();
string temp = $"{who}$Message${msg}$";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
}
public void FileSend(string filepath, string nick, string fileName)
{
NetworkStream ns = client.GetStream();
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
byte[] bytesToSend = new byte[fs.Length];
int numBytesRead = fs.Read(bytesToSend, 0, bytesToSend.Length);
int totalBytes = 0;
//Tutaj trzeba dodać, aby pobierał nazwę pliku
string temp = $"{nick}$File${fileName}${fs.Length}";
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(temp);
ns.Write(buffer, 0, buffer.Length);
Thread.Sleep(1000);
for (int i = 0; i <= fs.Length / BufferSize; i++)
{
if (fs.Length - (i * BufferSize) > BufferSize)
{
ns.Write(bytesToSend, i * BufferSize,
BufferSize);
totalBytes += BufferSize;
}
else
{
ns.Write(bytesToSend, i * BufferSize,
(int)fs.Length - (i * BufferSize));
totalBytes += (int)fs.Length - (i * BufferSize);
}
}
fs.Close();
}
}
}

Both client and server windows are blank

I am trying to test player position communication between a client and a server. I am doing this by building a game environment where the other player's position is also displayed. They connect properly, but both the windows are blank white. If I terminate one program only then the other displays the playground. Also in the action listener which listens for the timer only sends the data and doesn't proceed. Can I get help for displaying the windows properly and fixing the action listener?
Here is the client code :
public class SamplePlayerClient1
{
private static JFrame window = new JFrame("Sample Player Client 1");
private static class Ground extends JPanel {
private static final long serialVersionUID = 1L;
private int px = 50, py = 235, ex, ey, key;
private DataWriter writer;
private DataReader reader;
private ActionListener timerListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
writer.send(px, py);
System.out.println("data sent");
ex = reader.read().x;
ey = reader.read().y;
System.out.println("Data read");
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
};
private Timer animator = new Timer(30, timerListener);
private KeyAdapter keyListener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
key = e.getKeyCode();
if(key == KeyEvent.VK_UP) {
py -= 8;
if(py < 0) {
py = 0;
}
} else if(key == KeyEvent.VK_DOWN) {
py += 8;
if(py > 430) {
py = 430;
}
} else if(key == KeyEvent.VK_LEFT) {
px -= 8;
if(px < 0) {
px = 0;
}
} else if(key == KeyEvent.VK_RIGHT) {
px += 8;
if(px > 455) {
px = 455;
}
}
}
};
Ground() throws UnknownHostException, IOException {
Socket soc = new Socket(InetAddress.getByName("192.168.0.3"), 9998);
System.out.println("client 2 connected");
writer = new DataWriter(soc);
reader = new DataReader(soc);
animator.start();
addKeyListener(keyListener);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
requestFocus();
g.setColor(Color.BLACK);
g.fillRect(px, py, 30, 30);
g.setColor(Color.RED);
g.fillRect(ex, ey, 30, 30);
}
}
private static class DataWriter {
private OutputStreamWriter writer;
private String point = "";
DataWriter(Socket soc) throws IOException {
writer = new OutputStreamWriter(soc.getOutputStream());
}
void send(int x, int y) throws IOException {
point = "" + x;
point += ",";
point += y;
point += "\n";
writer.write(point);
writer.flush();
}
}
private static class DataReader {
private InputStreamReader is;
private BufferedReader reader;
private String point = "";
DataReader(Socket soc) throws IOException {
is = new InputStreamReader(soc.getInputStream());
reader = new BufferedReader(is);
}
Point read() throws IOException {
point = reader.readLine();
int x = Integer.parseInt(point.split(",")[0]);
int y = Integer.parseInt(point.split(",")[1]);
return new Point(x, y);
}
}
public static void main(String [] args) throws UnknownHostException, IOException {
Ground ground = new Ground();
window.setContentPane(ground);
window.setSize(600, 600);
window.setLocation(0, 0);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
I don't see the problem but I did see that:
ex = reader.read().x;
ey = reader.read().y;
This reads two lines and throws away the y from the first and the x from the second.
Should be:
Point p = reader.read();
ex = p.x;
ey = p.y;
The peer might not generate a point for you to read every time you write one so the reading of the points from the peer should not occur in the event handler that writes a point to the peer.

Camera 2 api full screen not to stretch

private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();
private static final long DELAY = 1000;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
private long startTime = 0L;
int num = 1;
List<VideoModel> videoList;
private Handler customHandler = new Handler();
private static final String TAG = "RecordVideoActivity";
private String videoName;
static {
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
static {
INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
}
private AutoFitTextureView mTextureView;
private ImageButton mRecordButton;
private ImageView mDots;
private ImageButton mCheckPoint;
private CameraDevice mCameraDevice;
private CameraCaptureSession mPreviewSession;
private TextureView.SurfaceTextureListener mSurfaceTextureListener
= new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
int width, int height) {
openCamera(width, height);
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
int width, int height) {
configureTransform(width, height);
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
private Size mPreviewSize;
private Size mVideoSize;
private MediaRecorder mMediaRecorder;
private boolean mIsRecordingVideo;
private HandlerThread mBackgroundThread;
private Handler mBackgroundHandler;
private Semaphore mCameraOpenCloseLock = new Semaphore(1);
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
#Override
public void onOpened(#NonNull CameraDevice cameraDevice) {
mCameraDevice = cameraDevice;
startPreview();
mCameraOpenCloseLock.release();
if (null != mTextureView) {
configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
}
}
#Override
public void onDisconnected(#NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
}
#Override
public void onError(#NonNull CameraDevice cameraDevice, int error) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
finish();
}
};
private Integer mSensorOrientation;
private String mNextVideoAbsolutePath;
private CaptureRequest.Builder mPreviewBuilder;
private CameraManager manager;
private String cameraId;
private boolean isFlashSupported;
private ImageButton flashButton;
private ImageButton switchCamera;
private ImageButton revertVideo;
public static final String CAMERA_BACK = "0";
private TextView mChronometer;
private String flashOpt;
private long lastSavedTime;
private String prepend;
private static Size chooseVideoSize(Size[] choices) {
for (Size size : choices) {
if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
return size;
}
}
Log.e(TAG, "Couldn't find any suitable video size");
return choices[choices.length - 1];
}
private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
// Collect the supported resolutions that are at least as big as the preview Surface
List<Size> bigEnough = new ArrayList<>();
int w = aspectRatio.getWidth();
int h = aspectRatio.getHeight();
for (Size option : choices) {
if (option.getHeight() == option.getWidth() * h / w &&
option.getWidth() >= width && option.getHeight() >= height) {
bigEnough.add(option);
}
}
// Pick the smallest of those, assuming we found any
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizesByArea());
} else {
Log.e(TAG, "Couldn't find any suitable preview size");
return choices[0];
}
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera2_video_image);
File dir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES) + File.separator + AppConstants.APPDIRRECORDING);
if (dir.isDirectory()) {
deleteDirectory(dir);
}
videoList = new ArrayList<>();
mTextureView = findViewById(R.id.textureView);
mRecordButton = findViewById(R.id.videoOnlineImageButton);
mRecordButton.setImageResource(R.drawable.ic_video);
mCheckPoint = findViewById(R.id.checkPoint);
mCheckPoint.setVisibility(View.GONE);
mDots = findViewById(R.id.dot);
mRecordButton.setOnClickListener(this);
flashButton = findViewById(R.id.flashVideo);
switchCamera = findViewById(R.id.switchVideo);
revertVideo = findViewById(R.id.revertVideo);
revertVideo.setVisibility(View.GONE);
mChronometer = findViewById(R.id.chronometer);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
flashOpt = prefs.getString(getString(R.string.flash_option), getString(R.string.auto));
if (flashOpt.equals(getString(R.string.flash_off)))
flashButton.setImageResource(R.drawable.flash_auto);
else if (flashOpt.equals(getString(R.string.flash_on)))
flashButton.setImageResource(R.drawable.flash_on);
else
flashButton.setImageResource(R.drawable.flash_off);
findViewById(R.id.checkPoint).setOnClickListener(this);
findViewById(R.id.flashVideo).setOnClickListener(this);
findViewById(R.id.switchVideo).setOnClickListener(this);
findViewById(R.id.revertVideo).setOnClickListener(this);
}
#Override
public void onResume() {
super.onResume();
startBackgroundThread();
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
if(mIsRecordingVideo)
mRecordButton.setImageResource(R.drawable.ic_video_stop);
else
mRecordButton.setImageResource(R.drawable.ic_video);
mCheckPoint.setEnabled(true);
startTime=SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
#Override
public void onPause() {
closeCamera();
mCheckPoint.setEnabled(false);
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
stopBackgroundThread();
super.onPause();
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int hour = mins / 60;
mChronometer.setText("" + hour + ":"
+ String.format("%02d", mins) + ":"
+ String.format("%02d", secs));
customHandler.postDelayed(this, 0);
}
};
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.videoOnlineImageButton: {
if (mIsRecordingVideo) {
mDots.setVisibility(View.GONE);
mChronometer.setVisibility(View.GONE);
switchCamera.setVisibility(View.VISIBLE);
mCheckPoint.setVisibility(View.GONE);
stopRecordingVideo();
Intent mediaStoreUpdateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaStoreUpdateIntent.setData(Uri.fromFile(new File(mNextVideoAbsolutePath)));
sendBroadcast(mediaStoreUpdateIntent);
} else {
mCheckPoint.setVisibility(View.VISIBLE);
switchCamera.setVisibility(View.GONE);
mChronometer.setVisibility(View.VISIBLE);
mDots.setVisibility(View.VISIBLE);
startRecordingVideo();
}
break;
}
case R.id.switchVideo: {
facingCamera = !facingCamera;
closeCamera();
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
break;
}
case R.id.flashVideo: {
if (onFlashCheck()) {
CameraCharacteristics cameraCharacteristics = null;
try {
cameraCharacteristics = manager.getCameraCharacteristics(cameraId);
} catch (CameraAccessException e) {
e.printStackTrace();
}
Boolean available = cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
isFlashSupported = available == null ? false : available;
switchFlash();
}
break;
}
}
}
private void startBackgroundThread() {
mBackgroundThread = new HandlerThread("CameraBackground");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
private void stopBackgroundThread() {
mBackgroundThread.quitSafely();
try {
mBackgroundThread.join();
mBackgroundThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#SuppressLint("MissingPermission")
private void openCamera(int width, int height) {
final Activity activity = this;
if (null == activity || activity.isFinishing()) {
return;
}
manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
Log.d(TAG, "tryAcquire");
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
cameraId = manager.getCameraIdList()[1];
if (facingCamera) {
cameraId = manager.getCameraIdList()[1];
flashButton.setVisibility(View.GONE);
} else {
cameraId = manager.getCameraIdList()[0];
flashButton.setVisibility(View.VISIBLE);
}
// Choose the sizes for camera preview and video recording
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics
.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
if (map == null) {
throw new RuntimeException("Cannot get available preview/video sizes");
}
mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
width, height, mVideoSize);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
} else {
mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
}
configureTransform(width, height);
mMediaRecorder = new MediaRecorder();
manager.openCamera(cameraId, mStateCallback, null);
} catch (CameraAccessException e) {
Toast.makeText(activity, "Cannot access the camera.", Toast.LENGTH_SHORT).show();
activity.finish();
} catch (NullPointerException e) {
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.");
}
}
private void closeCamera() {
try {
mCameraOpenCloseLock.acquire();
closePreviewSession();
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice = null;
}
if (null != mMediaRecorder) {
mMediaRecorder.release();
mMediaRecorder = null;
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.");
} finally {
mCameraOpenCloseLock.release();
}
}
private void startPreview() {
if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
return;
}
try {
closePreviewSession();
SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
Surface previewSurface = new Surface(texture);
mPreviewBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Collections.singletonList(previewSurface),
new CameraCaptureSession.StateCallback() {
#Override
public void onConfigured(#NonNull CameraCaptureSession session) {
mPreviewSession = session;
updatePreview();
}
#Override
public void onConfigureFailed(#NonNull CameraCaptureSession session) {
Toast.makeText(RecordVideoActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void updatePreview() {
if (null == mCameraDevice) {
return;
}
try {
setUpCaptureRequestBuilder(mPreviewBuilder);
HandlerThread thread = new HandlerThread("CameraPreview");
thread.start();
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void setUpCaptureRequestBuilder(CaptureRequest.Builder builder) {
if (onFlashCheck()) {
runOnUiThread(new Runnable() {
#Override
public void run() {
flashButton.setVisibility(View.VISIBLE);
}
});
if (flashOpt.equals(getString(R.string.auto)))
builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
else if (flashOpt.equals(getString(R.string.flash_off)))
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
else
builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
} else
runOnUiThread(new Runnable() {
#Override
public void run() {
flashButton.setVisibility(View.GONE);
}
});
}
private void configureTransform(int viewWidth, int viewHeight) {
if (null == mTextureView || null == mPreviewSize) {
return;
}
int rotation = getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
}
mTextureView.setTransform(matrix);
}
private void setUpMediaRecorder() throws IOException {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
File file = getVideoFilePath(this);
mNextVideoAbsolutePath = file.getAbsolutePath();
videoName = file.getName();
mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch (mSensorOrientation) {
case SENSOR_ORIENTATION_DEFAULT_DEGREES:
mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
break;
case SENSOR_ORIENTATION_INVERSE_DEGREES:
mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
break;
}
mMediaRecorder.prepare();
}
private File getVideoFilePath(Context context) {
File appDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES) + File.separator + AppConstants.APPDIRRECORDING);
if (!appDir.isDirectory()) {
appDir.mkdirs();
}
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String prepend = "VIDEO_" + timestamp + ".mp4";
File videoFile = null;
try {
videoFile = File.createTempFile(prepend, ".mp4", appDir);
} catch (IOException e) {
e.printStackTrace();
}
return videoFile;
}
private void startRecordingVideo() {
if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
return;
}
try {
closePreviewSession();
setUpMediaRecorder();
SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
List<Surface> surfaces = new ArrayList<>();
// Set up Surface for the camera preview
Surface previewSurface = new Surface(texture);
surfaces.add(previewSurface);
mPreviewBuilder.addTarget(previewSurface);
// Set up Surface for the MediaRecorder
Surface recorderSurface = mMediaRecorder.getSurface();
surfaces.add(recorderSurface);
mPreviewBuilder.addTarget(recorderSurface);
mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
#Override
public void onConfigured(#NonNull final CameraCaptureSession cameraCaptureSession) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPreviewSession = cameraCaptureSession;
updatePreview();
mIsRecordingVideo = true;
startTime = SystemClock.uptimeMillis();
mChronometer.setVisibility(View.VISIBLE);
customHandler.postDelayed(updateTimerThread, 0);
mRecordButton.setImageResource(R.drawable.ic_video_stop);
mMediaRecorder.start();
mCheckPoint.setEnabled(true);
}
});
}
#Override
public void onConfigureFailed(#NonNull CameraCaptureSession cameraCaptureSession) {
Toast.makeText(RecordVideoActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}, mBackgroundHandler);
} catch (CameraAccessException | IOException e) {
e.printStackTrace();
}
}
private void closePreviewSession() {
if (mPreviewSession != null) {
mPreviewSession.close();
mPreviewSession = null;
}
}
#SuppressLint("ResourceType")
private void stopRecordingVideo() {
mIsRecordingVideo = false;
customHandler.removeCallbacks(updateTimerThread);
mRecordButton.setImageResource(R.drawable.ic_video);
if(mMediaRecorder!=null) {
try {
mMediaRecorder.stop();
} catch (RuntimeException e) {
e.printStackTrace();
}
mMediaRecorder.reset();
}
}
static class CompareSizesByArea implements Comparator<Size> {
#Override
public int compare(Size lhs, Size rhs) {
return Long.signum((long) lhs.getWidth() * lhs.getHeight() -
(long) rhs.getWidth() * rhs.getHeight());
}
}
#Override
protected void onDestroy() {
customHandler.removeCallbacks(updateTimerThread);
}
I have implemented following coding in my app.i used camera 2 api for video recording.i feel that video streaming is stretched. mainly when i switch camera into front one.pls help me to resolve this problem
I have implemented following coding in my app.i used camera 2 api for video recording.i feel that video streaming is stretched. mainly when i switch camera into front one.pls help me to resolve this problem
Change aspect ratio values in AutoFitTextureView class or use Textureview instead of AutoFitTextureview.

Unity - UNET gameobject and child rotation

I've been trying for hours to fix this but now I have no idea what's wrong. Here is a video: https://www.youtube.com/watch?v=X8JrXwgvvFg As you can see my player is rotating and it's synced (as you can see by text above player), but the "hand" isn't and I don't know why. Can you help me?
Client.cs:
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;
public class Player
{
public string playerName;
public GameObject avatar;
public int connectionID;
}
public class ClientManager : MonoBehaviour {
private const int MAX_PLAYERS = 24;
private int port = 5701;
private int hostId;
private int reliableChannel;
private int unreliableChannel;
private int clientID;
private int connectionId;
private float connectionTime;
private bool isConnected = false;
private bool isStarted = false;
private byte error;
private string playerName;
public GameObject playerPrefab;
public Dictionary<int, Player> players = new Dictionary<int, Player>();
public void Connect()
{
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if (pName == "")
{
Debug.LogError("You must type in your nickname before you connect!");
return;
}
playerName = pName;
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_PLAYERS);
hostId = NetworkTransport.AddHost(topo, 0);
connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
connectionTime = Time.time;
isConnected = true;
}
private void Update()
{
if (!isConnected)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving: " + msg);
string[] splitdata = msg.Split('|');
switch (splitdata[0])
{
case "ASKNAME":
OnAskName(splitdata);
break;
case "CNN":
Debug.LogError(splitdata[1] + " connected!");
SpawnPlayer(splitdata[1], int.Parse(splitdata[2]));
break;
case "DC":
Debug.LogError(splitdata[1] + " disconnected!");
PlayerDisconnected(int.Parse(splitdata[2]));
break;
case "ASKPOSITION":
OnAskPosition(splitdata);
break;
default:
Debug.LogError("Wrong message: " + msg);
break;
}
break;
}
}
private void OnAskPosition(string[] data)
{
if (!isStarted)
return;
//Update everyone else
for (int i = 1; i < data.Length; i++)
{
string[] d = data[i].Split('%');
//Prevent to server from updating us
if (clientID != int.Parse(d[0]))
{
Vector3 position = Vector3.zero;
position.x = float.Parse(d[1]);
position.y = float.Parse(d[2]);
position.z = float.Parse(d[3]);
players[int.Parse(d[0])].avatar.transform.position = position;
players[int.Parse(d[0])].avatar.transform.rotation = Quaternion.Euler(0, float.Parse(d[4]), 0);
players[int.Parse(d[0])].avatar.transform.GetChild(0).rotation = Quaternion.Euler(float.Parse(d[5]), 0, 0);
Debug.LogWarning("THEIR Player rot: " + float.Parse(d[4]));
Debug.LogWarning("THEIR Camera rot: " + float.Parse(d[5]));
}
}
//Send our own position
Vector3 myPosition = players[clientID].avatar.transform.position;
Debug.LogWarning("MY Player rot Y: " + players[clientID].avatar.transform.rotation.eulerAngles.y.ToString());
Debug.LogWarning("MY Cam rot X: " + players[clientID].avatar.transform.GetChild(0).rotation.eulerAngles.x.ToString());
string m = "MYPOSITION|" + myPosition.x.ToString() + '|' + myPosition.y.ToString() + '|' + myPosition.z.ToString() + '|' + players[clientID].avatar.transform.rotation.eulerAngles.y.ToString() + '|' + players[clientID].avatar.transform.GetChild(0).rotation.eulerAngles.x.ToString();
Send(m, unreliableChannel);
}
Server.cs
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class Players
{
public int connectionId;
public string playerName;
public Vector3 position;
public float rotationPlayerY;
public float rotationCameraX;
}
public class ServerManager : MonoBehaviour
{
private const int MAX_PLAYERS = 24;
private int port = 5701;
private int hostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted = false;
private byte error;
private List<Players> players = new List<Players>();
private float lastmovementUpdate;
private float movementUpdateRate = 0.05f;
private void Start()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_PLAYERS);
hostId = NetworkTransport.AddHost(topo, port, null);
isStarted = true;
}
private void Update()
{
if (!isStarted)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.ConnectEvent:
Debug.Log("Player " + connectionId + " connected!");
PlayerConnect(connectionId);
break;
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving from " + connectionId + ": " + msg);
string[] splitdata = msg.Split('|');
switch (splitdata[0])
{
case "NAMEIS":
OnNameIs(connectionId, splitdata[1]);
break;
case "MYPOSITION":
OnMyPosition(connectionId, float.Parse(splitdata[1]), float.Parse(splitdata[2]), float.Parse(splitdata[3]));
OnMyRotation(connectionId, float.Parse(splitdata[4]), float.Parse(splitdata[5]));
break;
default:
Debug.LogError("Wrong message: " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent:
Debug.Log("Player " + connectionId + " disconnected!");
OnDisconnection(connectionId);
break;
}
//ask player for their position
if(Time.time - lastmovementUpdate > movementUpdateRate && players.Count > 0)
{
lastmovementUpdate = Time.time;
string m = "ASKPOSITION|";
foreach (Players pl in players)
{
//POSITION UPDATE
m += pl.connectionId.ToString() + '%' + pl.position.x.ToString() + '%' + pl.position.y.ToString() + '%' + pl.position.z.ToString() + '%' + pl.rotationPlayerY.ToString() + '%' + pl.rotationCameraX.ToString() + '|';
}
m = m.Trim('|');
Send(m, unreliableChannel, players);
}
}
private void OnMyPosition(int connID, float x, float y, float z)
{
players.Find(q => q.connectionId == connID).position = new Vector3(x, y, z);
}
private void OnMyRotation(int connID, float playerRot, float cameraRot)
{
players.Find(q => q.connectionId == connID).rotationCameraX = cameraRot;
players.Find(q => q.connectionId == connID).rotationPlayerY = playerRot;
}

Unity2D: UNET transport layer API server script isn't messaging properly

I'm following a tutorial unity multiplayer (UNET) on YouTube but I'm getting different results compared to the guy in the video. You see I followed his tutorial to the tea, however in my server script within the update function I used a debug.log in the switch statement default function to tell me if i'm receiving invalid messaging, which I am. I think that my message isn't really being sent properly despite the fact that I followed the guy's tutorial carefully! Am I missing something in my server script?
This is my code:
public class ServerClient
{
public int connectionId;
public string playerName;
}
public class Server : MonoBehaviour {
private const int MAX_CONNECTION = 4;
private int port = 8888;
private int hostId;
private int webHostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted = false;
private byte error;
private List<ServerClient> clients = new List<ServerClient>();
private void Start()
{
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
Debug.Log ("Socket Open. hostId is: " + hostId);
webHostId = NetworkTransport.AddWebsocketHost (topo, port, null); //, port, null
isStarted = true;
}
private void Update()
{
if (!isStarted)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData) {
case NetworkEventType.ConnectEvent: //2
Debug.Log ("Player " + connectionId + "has connected");
OnConnection (connectionId);
break;
case NetworkEventType.DataEvent: //3
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
//Debug.Log("Player" + connectionId + " has sent : " + msg);
Debug.Log("Recieving from " + connectionId + " : " + msg);
string[] splitData = msg.Split ('|');
switch (splitData[0])
{
case "OnNameIs":
OnNameIs (connectionId, splitData [1]);
break;
default:
Debug.Log ("Invalid message: " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent: // 4
Debug.Log("Player " + connectionId + "has disconnected");
break;
}
}
private void OnConnection(int cnnId)
{
// This may add a thrid player
ServerClient c = new ServerClient();
c.connectionId = cnnId;
c.playerName = "TEMP";
clients.Add(c);
//So you might want to change this later
string msg = "AskName|" + cnnId + "|";
foreach (ServerClient sc in clients)
msg += sc.playerName + '%' + sc.connectionId + '|';
msg = msg.Trim ('|');
Send (msg, reliableChannel, cnnId);
}
private void OnNameIs(int cnnId, string playerName)
{
clients.Find (x => x.connectionId == cnnId).playerName = playerName;
Send ("CNN|" + playerName + '|' + cnnId, reliableChannel, clients);
}
private void Send(string message, int channelId, int cnnId)
{
List<ServerClient> c = new List<ServerClient>();
c.Add (clients.Find (x => x.connectionId == cnnId));
Send (message, channelId, c);
}
private void Send(string message, int channelId, List<ServerClient> c)
{
Debug.Log ("Sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes (message);
foreach (ServerClient sc in c)
{
NetworkTransport.Send (hostId, sc.connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
}
}
The below script is my client script:
public class Player
{
public string playerName;
public GameObject avatar;
public int connectionId;
}
public class Client : MonoBehaviour {
private const int MAX_CONNECTION = 4;
private int port = 8888; //5701
private int hostId;
private int webHostId;
private int connectionId;
private int ourClientId;
private int reliableChannel;
private int unreliableChannel;
private float connectionTime;
private bool isConnected = false;
private bool isStarted = false;
private byte error;
private string playerName;
public GameObject playerPrefab;
public List<Player> players = new List<Player>();
public void Connect()
{
//Does the player have a name?
//take this part out
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if (pName == "") {
Debug.Log ("you must enter a name");
return;
}
playerName = pName;
//
// place this is the Start fuction
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, 0);
//
byte error;
connectionId = NetworkTransport.Connect (hostId, "127.0.0.1", port, 0, out error);
Debug.Log ("Connection to server. ConnectionId: " + connectionId);
connectionTime = Time.time;
isConnected = true;
}
private void Update()
{
if (!isConnected)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData) {
case NetworkEventType.DataEvent: //1
string msg = Encoding.Unicode.GetString (recBuffer, 0, dataSize);
Debug.Log ("Recieving : " + msg);
string[] splitData = msg.Split ('|');
switch (splitData[0])
{
case "AskName":
OnAskName (splitData);
break;
case "CNN":
SpawnPlayer (splitData[1], int.Parse(splitData[2]));
break;
case "DC":
break;
default:
Debug.Log ("Invalid message: " + msg);
break;
}
break;
}
}
private void OnAskName(string[] data)
{
ourClientId = int.Parse (data [1]);
Send ("OnNameis|" + playerName, reliableChannel);
for (int i = 2; i < data.Length - 1; i++)
{
string[] d = data[i].Split('%');
SpawnPlayer(d[0], int.Parse(d[1]));
}
}
private void SpawnPlayer(string playerName, int cnnId)
{
GameObject go = Instantiate (playerPrefab) as GameObject;
Debug.Log ("Object has been spawn", go);
if (cnnId == ourClientId) // the problem //
{
GameObject.Find("Canvas").SetActive (false);
isStarted = true;
}
Player p = new Player ();
p.avatar = go;
p.playerName = playerName;
p.connectionId = cnnId;
p.avatar.GetComponentInChildren<TextMesh>().text = playerName;
players.Add (p);
}
private void Send(string message, int channelId)
{
Debug.Log ("Sending : " + message);
byte[] msg = Encoding.Unicode.GetBytes (message);
NetworkTransport.Send (hostId, connectionId, channelId, msg, message.Length * sizeof(char), out error);
}
Thank you in advance!
Not quite to the tea.
If you look closely you'll see that:
The client sends a command with the name OnNameis
While the server expects a command named OnNameIs
This pretty much illustrates the dangers of using strings for commands. Use an Enum instead.