This repository has been archived by the owner on Aug 15, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 307
Send File
Peter Halasz edited this page Feb 13, 2019
·
2 revisions
Forge Networking works with all kinds of data, but that doesn't exclude sending files across the network. At the end of the day, files are just an array of bytes (byte[]
) and by now, you probably know you can send that stuff over the network with Forge Networking. So let's get started with a simple desktop->desktop file transfer shall we!
To get started we are going to need to setup our environment so that we can quickly and easily test our code. So let's open up a project with Forge Networking imported into it to get started.
- Download the
deal-with-it.jpg
image (cat image below) - Open project with Forge Networking imported
- Create a folder named
FileTransfer
- Copy the
deal-with-it.jpg
image into theFileTransfer
folder - Create a new scene and save it into the
FileTransfer
folder namedFileTransferTest
- Create a C# script in the
FileTransfer
folder namedSendThatFile
- Open that script in your favorite editor
- Put the following code into the script
Note that this code is not optimized but made simple for quick understanding
using BeardedManStudios;
using BeardedManStudios.Forge.Networking;
using BeardedManStudios.Forge.Networking.Frame;
using BeardedManStudios.Forge.Networking.Unity;
using System.IO;
using UnityEngine;
public class SendThatFile : MonoBehaviour
{
public string filePath;
private bool sentFile;
private void Start()
{
NetworkManager.Instance.Networker.binaryMessageReceived += ReceiveFile;
}
private void ReceiveFile(NetworkingPlayer player, Binary frame, NetWorker sender)
{
// We are looking to read a very specific message
if (frame.GroupId != MessageGroupIds.START_OF_GENERIC_IDS + 1)
return;
Debug.Log("Reading file!");
// Read the string from the beginning of the payload
string fileName = frame.StreamData.GetBasicType<string>();
Debug.Log("File name is " + fileName);
if (File.Exists(fileName))
{
Debug.LogError("The file " + fileName + " already exists!");
return;
}
// Write the rest of the payload as the contents of the file and
// use the file name that was extracted as the file's name
File.WriteAllBytes(fileName, frame.StreamData.CompressBytes());
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && !sentFile)
{
sentFile = true;
// Throw an error if this is not the server
var networker = NetworkManager.Instance.Networker;
if (!networker.IsServer)
{
Debug.LogError("Only the server can send files in this example!");
return;
}
// Throw an error if the file does not exist
if (!File.Exists(filePath))
{
Debug.LogError("The file " + filePath + " could not be found");
return;
}
// Prepare a byte array for sending
BMSByte allData = new BMSByte();
// Add the file name to the start of the payload
ObjectMapper.Instance.MapBytes(allData, Path.GetFileName(filePath));
// Add the data to the payload
allData.Append(File.ReadAllBytes(filePath));
// Send the file to all connected clients
Binary frame = new Binary(
networker.Time.Timestep, // The current timestep for this frame
false, // We are server, no mask needed
allData, // The file that is being sent
Receivers.Others, // Send to all clients
MessageGroupIds.START_OF_GENERIC_IDS + 1, // Some random fake number
networker is TCPServer);
if (networker is UDPServer)
((UDPServer)networker).Send(frame, true);
else
((TCPServer)networker).SendAll(frame);
}
}
}
- Open the Unity build menu
- Add the
MultiplayerMenu
scene as the first scene - Add the
FileTransferTest
as the second scene - Open the Player Settings...
- Turn on run in background
- Open the
FileTransferTest
scene - Select the camera or any object in the scene
- Add the
SendThatFile
script to the object - Update the
File Path
field on the script to have the full path to thedeal-with-it.jpg
image inside of theFileTransfer
folder - Save the scene
- Build and Run
- Open 2 instances of the game
- Set one to be the server then connect with the other
- Press the spacebar on the server
- Go into the build folder for the player and then go up a directory, you will notice a
deal-with-it.jpg
has been created
Getting Started
Network Contract Wizard (NCW)
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
Master Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
-
Connection Cycle Events
-
Rewinding
-
Network Logging
-
Working with Multiple Sockets
-
Modify Master and Standalone servers
-
NAT Hole Punching
-
UDP LAN Discovery
-
Offline Mode
-
Ping Pong
-
Lobby System
-
Upgrading Forge Remastered to Develop branch or different version
-
Forge Networking Classic to Remastered Migration Guide
-
Script to easily use Forge Networking from sources
-
Run Two Unity Instances with Shared Assets for Easiest Dedicated Client Workflow