admin管理员组文章数量:1323342
I'm just starting to play with Node.js today, and thought I'd start with what I thought would be a simple script: Connecting to a server via sockets, and sending a bit of data, and receiving it back. I'm creating a mand line utility. Nothing in the browser.
An example of a server would be memcached, beanstalkd, etc. It seems the net module is the right tool for the job, but I'm still a bit fuzzy on the Node.js way of doing things. Some help would be appreciated.
Update #1
Let me see if I can break this down in into a couple smaller questions. I hate even asking questions like this, but the Node.js documentation is very sparse, and most documentation written 6 months ago is already out dated.
1) So I can use net.stream.write() to send data to the remote server, but I don't know how to get a response back. I'm not even sure how to test when write() is finished, because it doesn't take a callback.
2) A few clues on how the whole event.emit thing works would be great. I think that's really the key stone I'm missing in those whole thing.
Update #2
Here's where I'm still confused on implementing a client program. Let me diagram a typical send request => get response system:
1) I bind callbacks to the net module to get responses and other events, including the necessary bindings to get a response from the server.
2) I use stream.write() to send a request to the server.
3) I then do nothing, because my bound "data" event will get the response from the server.
Here's where things get tricky. Suppose I call stream.write() twice before my bound "data" event is called. Now I have a problem. When the "data" event does happen, how do I know which of the 2 requests it's a response for? Am I guaranteed that responses will take place in the same order as requests? What if responses e back in a different order?
I'm just starting to play with Node.js today, and thought I'd start with what I thought would be a simple script: Connecting to a server via sockets, and sending a bit of data, and receiving it back. I'm creating a mand line utility. Nothing in the browser.
An example of a server would be memcached, beanstalkd, etc. It seems the net module is the right tool for the job, but I'm still a bit fuzzy on the Node.js way of doing things. Some help would be appreciated.
Update #1
Let me see if I can break this down in into a couple smaller questions. I hate even asking questions like this, but the Node.js documentation is very sparse, and most documentation written 6 months ago is already out dated.
1) So I can use net.stream.write() to send data to the remote server, but I don't know how to get a response back. I'm not even sure how to test when write() is finished, because it doesn't take a callback.
2) A few clues on how the whole event.emit thing works would be great. I think that's really the key stone I'm missing in those whole thing.
Update #2
Here's where I'm still confused on implementing a client program. Let me diagram a typical send request => get response system:
1) I bind callbacks to the net module to get responses and other events, including the necessary bindings to get a response from the server.
2) I use stream.write() to send a request to the server.
3) I then do nothing, because my bound "data" event will get the response from the server.
Here's where things get tricky. Suppose I call stream.write() twice before my bound "data" event is called. Now I have a problem. When the "data" event does happen, how do I know which of the 2 requests it's a response for? Am I guaranteed that responses will take place in the same order as requests? What if responses e back in a different order?
Share Improve this question edited Oct 19, 2010 at 21:36 mellowsoon asked Oct 19, 2010 at 3:12 mellowsoonmellowsoon 23.3k19 gold badges58 silver badges76 bronze badges 2-
Don't have much time ATM but net. nodejs/api.html clock on
net.Server
in the sidebar. There you have it. Everything is event based, you have to listen for thedata
event of the stream. The example actually is more a less a basic echo server. I can do some more examples for you later if you wish though. – Ivo Wetzel Commented Oct 19, 2010 at 7:16 -
In cases where it's important that you need to know on which
write
the client responded, you should probably add an id or something the like to the data you send/receive. But that's a general problem with networking and has nothing to do with the event based approach. – Ivo Wetzel Commented Oct 19, 2010 at 22:07
1 Answer
Reset to default 8First of all, let's make clear what a EventEmitter
is. JavaScript and therefore Node.js are asynchronous
. That means, instead of having to wait for ining connections on a server object, you add a listener
to the object and pass it a callback function
, which then, "as soon" as the event happens, gets executed.
There's still waiting here and there going on in the background but that has been abstracted away from you.
Let's take a look at this simple example:
// #1) create a new server object, and pass it a function as the callback
var server = net.createServer(function (stream) {
// #2) register a callback for the 'connect' event
stream.on('connect', function () {
stream.write('hello\r\n'); // as
});
// #3) register a callback for the 'data' event
stream.on('data', function (data) {
stream.write(data);
});
// #4) register a callback for the 'end' event
stream.on('end', function () {
stream.write('goodbye\r\n');
stream.end();
});
});
// #5) make the server listen on localhost:8124
server.listen(8124, 'localhost');
So we create the server and pass it the callback function
, this function is not yet executed. Passing the function here is basically a shortcut for adding a listener for the connection
event of the server object. After that we start the server at #5
.
Now what happens in the case of an ining connection?
Since the function we passed to
createServer
was bound to theconnection
event, it now gets executed.It adds the
connect
,data
andend
event listeners to thestream object
(which represents the individual connection) by hooking up callbacks for the events.After that, the
stream
fires theconnect
event, therefore the function passed at#2
gets executed and writeshello\r\n
to the stream. How does the function know which stream it should write to? Closures are the answer, the function inherits the scope it was created in, therefore inside the functionstream
is still referencing to the individual connection that triggered this very callback we're in right now.Now the client sends some data over the connection, which makes the
stream
object call itsdata
event, since we bound a function to this event at#3
we now echo the ining data back to the client.In case the client closes the connection, the function we've bound at
#4
gets called, which writesgoodbye\r\n
and after that closes the connection from our side.
Does this make things a little bit more clear? Well it definitely makes the whole thing a lot easier. Node is, just as well as JavaScript is inside Browsers, single threaded
. There's only one thing happening at a given point time.
To describe it simple, all these callbacks
end up in a global queue and are then called one after another, so this queue may(abstracted) look like this:
| connection event for a new stream
| data event for stream #12
| callback set via setTimeout
v close event of yet another stream
These are now get executed top to bottom, nothing will ever happen in between those. There's no chance, that while you're doing something in the callback
bound to the data
event, something will other will happen and magically change the state of the system. Even if there is a new ining connection on the server, its event will get queued up and it will have to wait until everything before it, including the data
event you're currently in, finishes.
本文标签: javascriptNodejs Connecting to a Server Using SocketsStack Overflow
版权声明:本文标题:javascript - Node.js: Connecting to a Server Using Sockets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742134525a2422310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论