Nodejs multi-Room web chat room and nodejsweb chat room

Source: Internet
Author: User
Tags set socket

Nodejs multi-Room web chat room and nodejsweb chat room

After a long period of time, I wrote a summary of the current PPT document. Source code download: https://github.com/CreekLou/chatRoom

Nodejs background

1. JavaScript was first run in a browser, but the browser only provides a context
2. node. js is actually another context, which allows you to run JavaScript code on the backend (out of the browser environment ).
3. In fact, Node. js is both a runtime environment and a library.

The Nodejs architecture is as follows:


The asynchronous mechanism of Node. js is based on events. All disk I/O, network communication, and database queries are requested in a non-blocking manner. The returned results are processed cyclically by the event.

• Event-driven callback (event polling) • asynchronous IO avoids frequent context switching • In addition to code in node, everything is executed in parallel

Multi-thread synchronous I/O and single-thread asynchronous I/O

Synchronous I/O (blocking ):
Multi-thread Throughput
Multi-core CPU utilization through event segmentation and Thread Scheduling
Multi-core CPU needs to be used by multiple threads scheduled by the operating system
Difficult to make full use of CPU resources
Large Memory tracks and weak Data Locality
Linear Programming

Asynchronous I/O (non-blocking ):
High throughput can be achieved with a single thread
Use multi-core CPU
You can bind a single process to a single-core CPU.
CPU resources can be fully utilized
Small memory trajectory and strong Data Locality
Not in line with traditional programming thinking


Node. js programs start from the event loop and end to the event loop. All logic is the Event Callback Function, so Node. js is always in the event loop. The program entry is the callback function of the first event in the event loop.


Node. js Core Module 1 is the heart of Node. js. It consists of some streamlined and efficient libraries and provides basic APIs for Node. js.
2. process: an object used to describe the state of the current Node. js process. It provides a simple interface with the operating system and is usually used when you write a local command line program.
3. console: provides standard console output. (IE)
4. Util: A Node. js core module that provides a set of common functions
5. events: The most important module of Node. js"
6. fs: file system, which provides POSIX file system operations such as file reading, writing, renaming, deletion, directory traversal, and linking.

Module httpServer,Nodejs server adopts V8 Virtual Machine

Var http = require ("http"); // request (require) Node. js built-in http module, and assign it to the http variable http. createServer (function (request, response) {// call the function provided by the http module: createServer response. writeHead (200, {"Content-Type": "text/plain"}); response. write ("Hello World"); response. end ();}). listen (0, 8888 );


Socket. io

Socket. io provides three default events: connect, message, and disconnect.

When a connection is established with the other party, the connect event is automatically triggered. when the other party receives the data, the message event is triggered (usually socket. send () triggered). When the other party closes the connection, the disconnect event is triggered.
Socket. emit (): Broadcast to the client that establishes the connection
Socket. broadcast. emit (): broadcast to all clients except clients that have established the connection.
Io. sockets. emit (): broadcast to all clients, equivalent to the sum

CLIENT (INDEX. HTML)

<script src="/socket.io/socket.io.js"></script> <script>varsocket=io.connect('http://localhost'); socket.on('news', function (data) {      console.log(data);       socket.emit('my other event', { my: 'data' }); }); </script>
SERVER (APP. JS)

Var app = require ('express ') (), server = require ('http '). createServer (app), io = require ('socket. io '). listen (server); // set socket. io is bound to the server. listen (80); app. get ('/', function (req, res) {res. sendfile (_ dirname + '/index.html') ;}); // The server listens to all clients and returns the new connection object io. sockets. on ('connection', function (socket) {socket. emit ('News', {hello: 'World'}); socket. on ('My other event', function (data) {console. log (data );});});

Chat Room function Diagram


References:

Http://socket.io/

Http://nodejs.org/documentation/tutorials/


Who can tell me how to change the single-user chat room system to multi-user multi-room chat room system with php? Tell me the detailed code (don't let me

If you are not proficient in using these technologies, practice more, take courses, familiarize yourself with production, then this will be very clear. You know, this is not a simple task. A large amount of investment is being made. In short, don't give up. I wish you success. Come on.
 
The JAVA-based WEB chat room system should be able to create a room and send information freely. If a user registers and logs in, if someone gives me a complete instance, all the naked messages will be sent

Dude, do you know how long this project will take? What is the value? In this case, give 30 points? I think it is unrealistic
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.