I have standard socket.io chat example running on linux server via nodejs.
var port = process.env.PORT || 81;
io.on('connection', function (socket) {
socket.on('new message', function (data) {
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});
}
Above we have Chat server, with part of code responsible for emmiting message to connected clients (new message). I want to be able to send such message from linux server directly to node chat server.
How to do that? For example with php script using socket connection? Example code how to do that i can think of
<?php
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
die();
}
echo "Socket created \n";
if(!socket_connect($sock , 'localhost' , 81)) {
die();
}
echo "Connection established \n";
$message = "new message\r\n\r\n";
//Send the message to the server
if( ! socket_send ( $sock , $message , strlen($message) , 0)) {
die();
}
echo "Message send successfully \n";
How to send properly new message in format like in chat server? (to include in message username and message itself)
$message = "<new message><username>assa</username><message>fsafsaasf</message></new message>\r\n\r\n";
or
$message = "new message\r\n\r\n"; ?