Hi, I use node for a lot of server side stuff. I’ve found using Express really helps when creating server side Rest API’s. Using tcp directly you need to remember a little bit more housekeeping, eg. buffSize = tcpClient.ReceiveBufferSize, that line you can’t assume is a whole message, If your message is greater than the MTU size it could be sent in multiple blocks. Localhost most likely has a larger MTU than ethernet, and that might explain why it fails on ethernet. Another advantage of using Express with it’s HTTP protocol, it’s nice for debugging, and putting an SSL layer on top is very easy.
If you still want to use TCP directly, I would suggest you send the length of your message first, and then the message so you can keep reading until you have received all data, then process your message.
Edit: Ok… I’ve now created a simple Chat Application, The code for both is now here and I’ve included a Zip [33854-chat.zip|33854] with the full example. In the Zip is two directories, one directory contains the unity project, the other contains a node express app. To save on zipping Express into the zip, go into this directory and type →
npm install express
And then to start the server.
node index
The meat of the code I’ll also paste here. In unity assign the C# script to say a dummy object, and then from your UI, assign the username / msg / textfield & button, and you should be good to go…
Node bit.
'use strict';
var express = require('express'),
events = require('events'),
app = express(),
eventer = new events.EventEmitter(),
comments = [];
//save having to doing json decoding in unity, do it here
function commentsTxt() {
var l, r = '', c;
for (l = 0; l < comments.length; l += 1) {
c = comments[l];
r += '<b>' + c.name + '</b> ' + c.msg + '
';
}
return r;
}
/* the list has a wait option, this will be usefull
for long polling */
app.get('/list', function (req, res) {
if (req.query.wait !== undefined) {
eventer.once('newComment', function () {
res.end(commentsTxt());
});
} else {
res.end(commentsTxt());
}
});
app.get('/say', function (req, res) {
if (comments.length >= 50) {
comments.splice(0, 1);
}
comments.push(req.query);
eventer.emit('newComment');
console.log(req.query);
res.json({saved: true});
});
app.listen(3000);
And the unity C# script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ChatController : MonoBehaviour {
public InputField fldUser;
public InputField fldMsg;
public Button btSend;
public Text textOutput;
private string urlapi = "http://discussions.unity.com/";
// Use this for initialization
IEnumerator Start () {
fldUser.value = "Player " + Random.Range(1, 100);
btSend.onClick.AddListener(sendMsg);
fldMsg.onSubmit.AddListener(sendMsg2);
textOutput.text = "Connecting..";
WWW www = new WWW(urlapi + "list");
yield return www;
textOutput.text = www.text;
StartCoroutine(longPolling());
}
IEnumerator longPolling () {
WWW www = new WWW(urlapi + "list?wait");
yield return www;
textOutput.text = www.text;
StartCoroutine(longPolling());
}
void sendMsg2(string msg) {
StartCoroutine(sendMsgNow());
}
void sendMsg() {
StartCoroutine(sendMsgNow());
}
IEnumerator sendMsgNow() {
if (fldMsg.value == "") {
fldMsg.ActivateInputField();
yield break;
}
string url = urlapi +
"say?name=" + WWW.EscapeURL(fldUser.value) +
"&msg=" + WWW.EscapeURL(fldMsg.value);
WWW www = new WWW(url);
yield return www;
fldMsg.value = "";
fldMsg.ActivateInputField();
}
}
As you can see, there is not a lot of code… It’s not a very complicated chat app deliberately to make it easy to follow… Hope this helps to get you started…