Reputation: 479
I need to create a web app which can communicate with a CasparCG server. The server accepts TCP connections with basic IP & port. You then send strings through the connection which will then trigger stuff.
I've stumbled onto a dilemma as to use PHP as the main communication tool for this because it appears every time PHP reads a response with fread it disconnects from the server. The script ends, so the connection terminates.
I have very little experience with this portion of PHP or TCP connections with any language. I don't know if the disconnection will affect overall performance making every request delayed as to a C# TCP connection which seems to be able to stay alive and disconnect when the application closes or connection is aborted. This is why I read about node.js.
Now, would the better alternative for such an application be to use Node.js as to PHP? I've already got a good basis to start with AngularJS + PHP but PHP is the problem. I have no experience with node.js but I'm willing to have a go at it.
Upvotes: 0
Views: 210
Reputation: 128
Yes, I suggest you to use Node.js. You can pick up the Net module.
var s = require("net");
var cn = s.createConnection({port: YourPortHere, host: YourHostHere}, callback); // createConnection and connect should be the same (cn is instance of net.Socket)
function callback(){
// Called when the connection is ready and so cn can be used
}
cn.on("data", function(data){
// Handle messages received by server (data is instance of Buffer)
if(data.toString("ascii") == "ping"){
s.write("pong!");
}
});
cn.on("end", function(){
// Disconnected
});
As you can see is really simple to use. Note: if you need to send or parse special characters you can change the encoding, for example, in line 10 of receiving callback:
if(data.toString("utf8") == "èùà") // BTW, the parameter is usually set by default to utf8, so there is no need to provide that, in theory.
To send special characters
cn.setEncoding("utf8");
Upvotes: 1