0

it worked fine with out joining a room a list of friends is displayed and on click, a new page is opened for chatting for individual friends

<table>
    <tr>
      <th>Name</th>

    </tr>
    {{#each friends}}
    <tr>
      <td><a href="/chat?name={{this.profileName}}">{{this.profileName}}</a></td>

    </tr>
    {{/each}}
  </table>

then on chat_server.js where socket.io resides

socket.join(chatTable);//joining a room ! where problem rises

io.sockets.in(chatTable).emit('db chat message',rows);  

then on web side

 <form action="">
  <input id="m" autocomplete="off" /><button>Send</button>
</form>

and on the jquery portion

 $('form').submit(function(){

  socket.emit('chat message', $('#m').val());
   $('#m').val()='';
  return false;
});

when joining a room is invoked... the page is refreshed with the url parameter(name) being gone and the socket.io being disconnected and reconnected.

4

1 回答 1

1

I assume the chatTable variable is a string.

Use event.preventDefault():

 $('form').submit(function(event) {

    event.preventDefault();
    socket.emit('chat message', $('#m').val());
    $('#m').val("");//$('#m').val()=' ' ; does not work 
});

Also, check the Socket.IO cheatsheet and the docs of rooms and namespaces.

于 2018-07-23T10:36:46.530 回答