This article shares the specific code for implementing a simple chat room conversation using websocket for your reference. The specific content is as follows First, build a node environment and write the following code in app.js npm install socket.io-client Socket is a high-performance server framework. Developers can develop their own network applications, such as RPC services, chat room servers, mobile game servers, etc., by implementing one or two interfaces. npm install http-server Generally, server services are provided. Parameters can specify ports, addresses, etc. For example, to start a service at port 8888, the command is: http-server src -p 8888 npm install koa Koa implements a very expressive HTTP middleware framework through node.js, striving to make web application development and API usage more pleasant. Koa's middleware is executed in the stack in the order in which it is encoded, allowing you to perform operations and pass requests downward (downstream), then filter and return responses in reverse order (upstream). Implementation Code// Introduce dependencies const koa = require("koa") // Initialize koa const app = new koa() // Enable http var server = require("http").createServer(app.callback()) // Initialize the socket const io = require("socket.io")(server, { cors: true }) // Listen io.on('connection', (socket) => { // Actively send messages to the client setTimeout(() => { // Trigger a custom event through the io object method emit and send a message to the client io.emit('chat message', 'What do you want to say?') }, 1000) socket.on('disconnect', () => { console.log('user disconnected') }) // Receive the client's message on the server // Listen to the event through the on method. When the client sends a message, the event will be triggered and the message sent by the client can be received socket.on('chat message', (msg) => { console.log(msg) // msg is the message sent by the client // Sending a message to the client is emit setTimeout(() => { msg = msg.replace("you", "me").replace("?", "").replace("?", "!") // Trigger event to send the processed message to the client io.emit('chat message', msg) }, 500) }) }) server.listen(5522,()=>{ console.log('socket service is enabled, port number is 5522') }); Call this service import { io } from 'socket.io-client' cteated(){ // 1. Create a connection that can be customized this.socket = io('ws://localhost:5522') // 2. Establish a connection this.socket.on('connect', () => { console.log('Connection established successfully') }) // 3. Listen for messages and return this.socket.on('chat message', msg => { console.log('Message returned by the service', msg) }) } This will allow you to have a simple AI conversation. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Mycli is a must-have tool for MySQL command line enthusiasts
>>: In-depth explanation of the locking mechanism in MySQL InnoDB
I don’t know if you have noticed that when we ope...
The previous article introduced several methods f...
1. Replace your .js library file address with the...
Recently, when I was learning how to use webpack,...
Batch comments in SQL Server Batch Annotation Ctr...
For historical reasons, MySQL replication is base...
This article shares the specific code for randomi...
I had always wanted to learn Kubernetes because i...
Table of contents 1. Problem Discovery 2. View de...
Tomcat accesses the project, usually ip + port + ...
1. Time types are divided into: 1. Network time (...
1. Download the installation package The installa...
The reason is this I wanted to deploy a mocker pl...
【question】 When the outer table and the inner tab...
Table of contents Preface Global parameter persis...