socket-io.js 689 B

12345678910111213141516171819202122232425
  1. const io = require('socket.io-client');
  2. // Connect to the Socket.IO server
  3. // const socket = io('http://127.0.0.1:3000');
  4. const socket = io('http://127.0.0.1:3000/Sample-App');
  5. // Listen for the 'connect' event
  6. socket.on('connect', () => {
  7. console.log('Connected to the server!');
  8. // Emit a test event to the server
  9. setTimeout(() => {
  10. socket.emit('message', { message: 'Hello from the client!' });
  11. }, 3000)
  12. });
  13. // Listen for a 'test-response' event from the server
  14. socket.on('message', (data) => {
  15. console.log('Received from server:', data);
  16. });
  17. // Listen for the 'disconnect' event
  18. socket.on('disconnect', () => {
  19. console.log('Disconnected from the server');
  20. });