2016年1月28日 星期四

【Node.js】LEVEL 6 - Socket.io

6.2 Setting Up socket.io Server-Side

So far we've created an Express server. Now we want to start building a real-time Q&A moderation service and we've decided to use socket.io.

Task 1:
Using the http module, create an new http server and pass the express app as the listener for that new server.

Task 2:
Using the socket.io module, listen for requests on the http server. Store the return object of this operation in a variable called io.

Task 3:
Use the object stored in io to listen for client 'connection' events. Remember, the callback function takes one argument, which is the client object that has connected.

Task 4:
When a new client connects, log a message using console.log().

Task 5:
Finally, we want to tell our http server to listen to requests on port 8080.

```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(client){ console.log(client + "has connected."); }); server.listen(8080); ```

6.3 Client socket.io Setup

In our html file, load the socket.io.js script and connect to the socket.io server.

Task 1:
Load the socket.io.js script. The socket.io.js path you should use is '/socket.io/socket.io.js'. Express knows to serve the socket.io client js for this path.

Task 2:
Using the global io object that's now available for us, connect to the socket.io server at http://localhost:8080.

```js ```

6.4 Listening For Questions

In our client below, listen for 'question' events from the server and call the insertQuestion function whenever the event fires.

Task 1:
First, listen for 'question' events from the server.

Task 2:
Now, have the event callback function call the insertQuestion function. The insertQuestion function is already created for you, and it's placed in its own file. It expects exactly one argument - the question.

```js ```

6.5 Broadcasting Questions

When a question is submitted to our server, we want to broadcast it out to all the connected clients so they can have a chance to answer it.

Task 1:
In the server, listen for 'question' events from clients.

Task 2:
Now, emit the 'question' event on all the other clients connected, passing them the question data.

```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(client) { console.log("Client connected..."); client.on('question', function(question){ client.broadcast.emit('question', question); }); }); server.listen(8080); ```

6.6 Saving Client Data

In our real-time Q&A app, we want to allow each client only one question at a time, but how do we enforce this rule? We can use socket.io's ability to save data on the client, so whenever a question is asked, we first want to check the question_asked value on the client.

Task 1:
First, when a client emits a 'question' event, we want to set the value of question_asked to true.

Task 2:
Second, when a client emits a 'question' event, we want to broadcast that question to the other clients.

Task 3:
Finally, when a client emits a 'question' event, check to make sure question_asked is not already set to true. We only want to allow one question per user, so make sure that we only set the value of question_asked and broadcast the question to other clients when the value of question_asked is not already true.

```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(client) { console.log("Client connected..."); client.on('question', function(question) { if(!client.question_asked){ client.question_asked = true; client.broadcast.emit('question', question); } }); }); server.listen(8080); ```

6.7 Answering Questions

Clients can also answer each other's questions, so let's build that feature by first listening for the 'answer' event on the client, which will send us both the question and answer, which we want to broadcast out to the rest of the connected clients.

Task 1:
With the client, listen for the 'answer' event from clients.

Task 2:
Now, emit the 'answer' event on all the other clients connected, passing them the question data.

```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.sockets.on('connection', function(client) { console.log("Client connected..."); client.on('answer', function(question, answer){ client.broadcast.emit('answer', question, answer); }); client.on('question', function(question) { if(!client.question_asked) { client.question_asked = true; client.broadcast.emit('question', question); } }); }); server.listen(8080); ```

6.8 Answering Question Client

Now on the client, listen for the 'answer' event and then broadcast both the question and the answer to the connected clients.

Task 1:
Listen for the 'answer' event off of the server.

Task 2:
Call the answerQuestion function, passing in both the question and the answer that was broadcast from the server.

```js ```

2 則留言:

  1. Hey That was a great read, very informative, though native java debugging is not something I am good at i really liked this article, Check this out for a little more info.
    Also don't forget to check out this one: Node JS Development Company

    回覆刪除
  2. Nice content here you see more content about this topic nodejs development company

    回覆刪除