22 lines
		
	
	
		
			551 B
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			22 lines
		
	
	
		
			551 B
		
	
	
	
		
			JavaScript
		
	
	
	
| // Import Socket.IO client library
 | |
| const io = require('socket.io-client');
 | |
| 
 | |
| // URL of the Socket.IO server
 | |
| const serverUrl = 'http://localhost:3000';
 | |
| 
 | |
| // Connect to the server
 | |
| const socket = io(serverUrl);
 | |
| 
 | |
| // Event handler for successful connection
 | |
| socket.on('connect', () => {
 | |
|     console.log('Connected to server');
 | |
| });
 | |
| 
 | |
| // Event handler for receiving messages from the server
 | |
| socket.on('message', (data) => {
 | |
|     console.log('Received message from server:', data);
 | |
| });
 | |
| 
 | |
| // Sending a message to the server
 | |
| socket.emit('message', 'Hello, server!');
 |