i have a console app, which connects to a server, prints output to the console.
here is the code made with Node js
var net = require('net');
var client = new net.Socket();
client.connect(port, 'ip', function() {
console.log('Connected');
client.write('enter password...');
});
client.on('data', function(data) {
console.log('Received: ' + data);
});
i can login successfully and read the output sent by the server on console based application, but i want to see these outputs on webpage. i tried using express but i have no clue how to use it.
app.get('/', function (req, res) {
client.on('data', function(data) {
res.send('<p>: ' + data + '</p>');
});
});
can anyone tell me where i am wrong? or should i use something else?
Related
In one Screen i am getting total amount.
If they Click on pay in machine option.
I have to send that amount through tcp server to pinelab machine after payment i have to get response as payment success.
const rawData = '<ORIGINAL_TRANS_ID>1718512243204</ORIGINAL_TRANS_ID><AMOUNT>615</AMOUNT></TRANS>';
var uint8array = new TextEncoder("utf-8").encode(rawData);
var mString = new TextDecoder().decode(uint8array);
var client = new net.Socket();
client.connect(0000, '127.0.0.1', function() {
console.log('Connected');
client.write(mString);
//console.log(mString);
});
client.on('data', function(data) {
console.log('Received: ' + data.toString());
client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
i have to send amount and trsns id here dynamically uisng reacj and node js
Node js net module server code:
var net = require('net');
var server = net.createServer(function (connection) {
console.log('client connected');
connection.on('data', function (data) {
console.log('data from flash = ' + data);
var jsonData = {};
jsonData.message = "joined";
var d = JSON.stringify(jsonData);
connection.write(d);
});
connection.on('end', function () {
console.log('client disconnected');
});
// connection.pipe(connection);
});
server.listen(3055, function () {
console.log('server is listening');
});
Action script code
this.login_socket.connect(this.server_ip,3055);
this.login_socket.addEventListener(Event.CONNECT,this.login_socket_onConnection);
this.login_socket.addEventListener(DataEvent.DATA,this.login_onData);
this.login_socket.addEventListener(IOErrorEvent.IO_ERROR,this.login_socket_onIOErrorEvent);
this.login_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.login_socket_SecurityErrorEvent);
this.login_socket.addEventListener(Event.CLOSE,this.login_socket_closeErrorEvent);
Can anyone please tell me how to use xml socket with node js net module? I have tried everything but this doesn't work at all. I want to create a socket connection for a flash game to the server. I am using laravel as backend. If anyone knows how to create it with php tell me. Thank you.
var net = require('net');
var server = net.createServer(function (connection) {
console.log('client connected');
connection.setEncoding("utf8");
// on receive data from client
connection.on('data', function (data) {
console.log('data from flash = ' + data);
var d = JSON.stringify({
message: "joined"
}) + "\0";
connection.write(d);
});
// on connection end
connection.on('end', function () {
console.log('client disconnected');
});
// on error
connection.on("error", function (exception) {
console.log('an error occurred: ' + exception);
});
});
server.listen(3055, function () {
console.log('server is listening');
});
I change nodejs server code to above code and it works fine now.
I'm listening all the connections from a GPS device who is send the data to a our server. So I create a small NodeJs app to retrieve that information.
var net = require('net');
var server = net.createServer(function(connection) {
connection.on('drain', function(){
console.log('drain');
});
connection.on('data', function(data){
var dataGps = data.toString('ascii');
var fecha = new Date();
console.log(String(fecha));
var dataArray = dataGps.split(',');
});
connection.on('end', function() {
//console.log('Client disconnected'.magenta);
});
connection.on('error', function(err) {
//console.log("Connection error: " + err+" ... ".red);
//console.log('Closing connection....'.red);
//connection.destroy();
});
connection.on('close', function(){
console.log('closed event fired');
});
});
server.on('connection', function(){
console.log('new connection');
});
server.getConnections(function(err, count){
if (err) {
console.log('# Error: '+err);
}
console.log('Count: '+count);
});
server.listen(3001, function() {
console.log('Server is listening on port 3001...');
});
To keep running this App, I'm using pm2 (pm2 website).
Everything is fine except that sometimes this app becomes inactive and don't do anything until I restart it with pm2.
This app is in Amazon EC2 (t2.medium instance) Ubuntu 16.04.
I don't know what I'm doing wrong. Any help would be really helpful.
Tried different methods, but the data is sent to a maximum of one or two clients. How to send data to all the clients connected to the server ? What am I doing wrong?
Server.js:
var PORT = 3000;
var options = {
// 'log level': 0
};
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server, options);
server.listen(PORT);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/attantions/templates/.default/template.php');
});
io.sockets.on('connection', function (client) {
client.on('attantion', function (data) {
try {
// Tried so
io.sockets.volatile.emit('attantion', data);
// And tried so
io.sockets.emit('attantion', data);
client.emit('attantion', data);
client.broadcast.emit('attantion', data );
} catch (e) {
console.log(e);
client.disconnect();
}
});
});
Client.js:
socket.emit("attantion", data);
socket.on('attantion', function (data) {
pushData(data);
});
See this post for different options for socket.io messages
Send response to all clients except sender (Socket.io)
io.sockets.on('connection', function (client) {
client.on('attantion', function (data) {
//client.emit('attantion', data ); // This will send it to only the client
//client.broadcast.emit('attantion', data); // This will send it to everyone but this client
io.emit('attantion', data); // This will send it to all attached sockets.
});
});
Edit
I wonder if this post can help you?
Socket.io - Cannot load file
I was curious how sending the php file to the client through node.js works? are you using another framework?
Could you show more of what your client code looks like? loading the lib and the instantiation of the socket.
I am working on a socket.io + Node.js project.
When I print an variable using console.log, I get my variable on console.
But when I sent this variable to a client, it seems as [object Object] form on browser.
How can I see my variable on browser?
Thanks
Try console.log(JSON.stringify(myVariable)); and then look at it in your browser and you'll gain more insight as to what's happening exactly.
You may be using the 0.6 serverside syntax of:
socket.send({foo: 'bar'})
Try using the following updated syntax:
socket.json.send({foo: 'bar'})
You can find more information here:
https://github.com/LearnBoost/Socket.IO/wiki/Migrating-0.6-to-0.7
thanks, console.log(JSON.stringify(myVariable)); worked for my case. Variable has shown as
{ coloumn_that_result_lays: "result"}. Of course we can overcome it with javascript-substring but is there a function which gives "result" directly.
// Require HTTP module (to start server) and Socket.IO
var io = require('socket.io'),
http = require('http');
io = require('socket.io');
// Start the server at port 8080
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end('<h1>Hello Socket Lover!</h1>');
});
server.listen(8080);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);
// Add a connect listener
socket.on('connection', function (client){
//mysql
var mysql = require('db-mysql');
new mysql.Database({
hostname: 'localhost',
user: 'root',
password: '123123',
database: 'node'
}).connect(function(error) {
if (error) {
return console.log('CONNECTION error: ' + error);
}
this.query('SELECT data FROM exam where id=2 ').
execute(function(error,result) {
if (error) {
console.log('ERROR: ' + error);
return;
}
client.json.send(JSON.stringify(result));
});
});
// Create periodical which ends a message to the client every 5 seconds
var interval = setInterval(function() {
client.send('This is a message from the server! ' );
},1000);
// Success! Now listen to messages to be received
client.on('message',function(event){
console.log('Received message from client!',event);
});
client.on('disconnect',function(){
clearInterval(interval);
console.log('Server has disconnected');
});
});