How to create a simple chat room with Node.js
This article mainly introduces the relevant information of Node.js to create a simple chat room in detail. It has certain reference value. Interested friends can refer to
to read the relevant knowledge of TCP. , wrote a chat room based on NET.
var net = require('net'); var count = 0, users = {}; var server = net.createServer(function (conn) { console.log('连接到'); conn.write( "\r\n > welcome to node-chat!" + "\r\n > " + count + " other people are connected at this time." + "\r\n > please write your name and press enter: " ); count++; // 代表当前连接的昵称 var nickname; console.log(conn); conn.on('data', function (data) { // 删除\r\n // data = data.replace('\r\n', ' '); // 当前注册的昵称不存在 if (!nickname) { // 用户名存在 if(users[data]) { conn.write('nickname in use'); return; } else { // 用户名给nickname nickname = data; users[nickname] = conn; for (var i in users) { users[i].write('\r\n > ' + nickname + ' join our room \r\n > I: '); } } } else { // 开始聊天 for (var i in users) { if (i != nickname) { users[i].write('\r\n > ' + nickname + ': ' + data); } } } }); conn.on('close', function () { count--; }); conn.setEncoding('utf8'); }); server.listen(3000, function () { console.log('服务器监听端口3000'); })
Running screenshot:
Terminal:
telnet Here are two screenshots Netizen
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
angular2 and nodejs implement the image upload function
SpringBoot and Vue.js before and after implementation End-detached file upload function
#
The above is the detailed content of How to create a simple chat room with Node.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

How to use MySQL and Java to implement a simple chat room function Introduction: With the prevalence of social media today, people increasingly rely on online chat to communicate and share information. How to implement a simple chat room function using MySQL and Java is a very interesting and practical project. This article will introduce how to use MySQL and Java to implement this function, and provide specific code examples. 1. Build a database First, we need to create a database in MySQL to store chat room related information.

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

How to use Go language to develop a Websocket chat room. Websocket is a real-time communication protocol that allows two-way communication between the server and the client by establishing a connection. Websocket is a very good choice when developing chat rooms because it enables real-time message exchange and provides efficient performance. This article will introduce how to develop a simple Websocket chat room using Go language and provide some specific code examples. 1. Preparation 1. Install Go
