最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to run node.js client on browser - Stack Overflow

programmeradmin3浏览0评论

everyone

I'm very new to node.js. I'm trying to do a tcp server <-> client using node.js. So far so good. The server script can be run Ok. Also the client script can be run OK.

But the problem is I could only get the client to run from the terminal by typing command (node client.js).
The thing is I would like to run it in a browser so I could take the data received from server display on browser.

How do I do that?

Please help.

Kawin.

This is the client code. (I can't remember who originally created this script. I copy and paste it from somewhere but forget to bookmark from which I get the link. Sorry for not putting the credit to the owner of this script.)

var net = require('net');

var HOST = '192.168.0.88';
var PORT = 8888;

var client = new net.Socket();
client.connect(PORT, HOST, function() {

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    // Write a message to the socket as soon as the client is connected, the   server will receive it as message from the client 
    client.write('B2\r\n');
});

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
    console.log('DATA: ' + data);
    // Close the client socket completely
    client.destroy();
});

// Add a 'close' event handler for the client socket
client.on('close', function() {
    console.log('Connection closed');
}); 

Thank you.

everyone

I'm very new to node.js. I'm trying to do a tcp server <-> client using node.js. So far so good. The server script can be run Ok. Also the client script can be run OK.

But the problem is I could only get the client to run from the terminal by typing command (node client.js).
The thing is I would like to run it in a browser so I could take the data received from server display on browser.

How do I do that?

Please help.

Kawin.

This is the client code. (I can't remember who originally created this script. I copy and paste it from somewhere but forget to bookmark from which I get the link. Sorry for not putting the credit to the owner of this script.)

var net = require('net');

var HOST = '192.168.0.88';
var PORT = 8888;

var client = new net.Socket();
client.connect(PORT, HOST, function() {

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    // Write a message to the socket as soon as the client is connected, the   server will receive it as message from the client 
    client.write('B2\r\n');
});

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
    console.log('DATA: ' + data);
    // Close the client socket completely
    client.destroy();
});

// Add a 'close' event handler for the client socket
client.on('close', function() {
    console.log('Connection closed');
}); 

Thank you.

Share Improve this question edited Feb 4, 2016 at 10:06 Markus Safar 6,5805 gold badges31 silver badges45 bronze badges asked Jan 21, 2015 at 4:13 Kawin NetvichitpanKawin Netvichitpan 1031 gold badge1 silver badge6 bronze badges 2
  • 1 "How do I do that?" You can't. Node.js and the browser are two different environments. If you use a feature that is unique to an environment (such as TCP sockets), you can't use the same script in other environments. – Felix Kling Commented Jan 21, 2015 at 4:20
  • Thank you. so i think i might have to change my plan to acheive this. – Kawin Netvichitpan Commented Jan 21, 2015 at 6:32
Add a comment  | 

3 Answers 3

Reset to default 12

Node.js is not browser javascript. There are many parts of it that use OS features not available in a browser context. The way to do what you're looking to do while staying in the browser for the client, is to not use a TCP socket, but instead look into WebSockets (e.g. socket.io, which offers server and browser clients).

Times are changing. It's just been announced that it might be possible to use node.js in the browser soon. Check out this link: Run Node.js in the browser

The thing is I would like to run it in a browser so I could take the data received from server display on browser.

I think you need 'http' module.

var http=require('http');
var server=http.Server(function(req,res) {
    res.end('<p>hello world</p><script>alert("hello world")</script>');
});

server.listen(8080);

so you can get data from browser side by typing URL 'localhost:8080'

发布评论

评论列表(0)

  1. 暂无评论