-1

client code 1

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <script src="/socket.io/socket.io.js"></script>
  <title>Document</title>
</head>

<body>
  <button id="my_button">sending msg to same room client</button>

  <script>
    var roomNumber = prompt('what`s your roomNumber.');

    const socket = io.connect();

    socket.emit('joinABCD', {
      roomNumber: roomNumber,
    });

    **
    var button = document.getElementById("my_button");
    button.onclick = function() {
      socket.emit('message', {
        message: 'can you see me roomnumber people?'
      });
    }; **

    //no jquery accept in node.hs . ==>how to soleve?  


    socket.on('message2', (data) => {
      alert(data.message);
    });
  </script>
</body>

</html>

server node code

const express = require('express');
const http = require('http');
const socketIo = require('socket.io');

const app = express();

// app.use(express.static(`${__dirname}/public`));
const server = http.createServer(app).listen(2000, () => {
  console.log('http://127.0.0.1:2000');
});

app.get('/', (request, response) => {
  response.sendFile(`${__dirname}/public/creatingRoom.html`);
});

const io = socketIo.listen(server);

io.sockets.on('connection', (socket) => {
  let roomNumber = null;

  socket.on('joinABCD', (data) => {
    roomNumber = data.roomNumber;
    socket.join(data.roomNumber);
  });

  socket.on('message', (data) => {
    io.sockets.in(roomNumber).emit('message2', {
      message: `${data.message}`,
    });
  });
});

client code1 and sever code matching each other then it work without problem. but if i changed code1 to code2 it doesn't work. code2 below

from client code1

var button = document.getElementById("my_button");
button.onclick = function() {
  socket.emit('message', {
    message: 'can you see me roomnumber people?'
  });
};

to client code2

$("#my_button").click(function() {
  socket.emit('message', {
    message: 'can you see me roomnumber people?'
  });
});

how can i operate jquery source in serverside . plz teach me how to do it. some people said me i have to module cheerio is that right?

Manish Balodia
  • 1,863
  • 2
  • 23
  • 37
백광일
  • 3
  • 2
  • my jquery source operate well in only client html document. but if i open it in node server , then it never work – 백광일 Aug 27 '18 at 02:32
  • You don't appear to have any reference to the jQuery library whatsoever in your sample code. – Obsidian Age Aug 27 '18 at 02:32
  • 4
    Possible duplicate of [Can I use jQuery with Node.js?](https://stackoverflow.com/questions/1801160/can-i-use-jquery-with-node-js) – Obsidian Age Aug 27 '18 at 02:33
  • 1
    Your jquery you show isn't operating server-side. You're attempting to run it in the web page which runs in the browser. It will work if you include the jquery library in your web page with an appropriate ` – jfriend00 Aug 27 '18 at 03:53
  • You ask why jquery doe snot work server side, but in `server node code` there is no jQuery used, and `client code1` and `client code2` are as you already say client code and do not run on the client. So why do you think it is in any way related to the server? And what do you mean with `client code1 and sever code matching each other`, what it the matching? – t.niese Aug 27 '18 at 07:43

1 Answers1

0
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    **<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.slim.js"></script>
    <title>Document</title>**
</head>
<body>
    <button>메시지 전송</button>
    <script>
    var roomNumber = prompt('방번호를 입력하세요.');

     const socket = io.connect();
     socket.emit('joinABCD',{
         roomNumber:roomNumber,
     })

     $(document).ready(function(){
         $('button').click(function(){
             socket.emit('message',{
                 message: '우리방 사람들 보이세요?',
             });
         });
     });


     socket.on('message2' ,(data) => {
         alert(data.message);
     });
    </script>
</body>
</html>

The reason why jquery doesn`t operate is just not to put cdn in script code.

and i realized websocket script tag src code src="/socket.io/socket.io.js" never meaning that real src but sudo src, it makes me confused.

/socket.io/socket.io.js code itself CDN

it means almost same that .

백광일
  • 3
  • 2