Используйте socket.io в веб-чате Servlet / jsp - PullRequest
0 голосов
/ 20 октября 2018

Я новичок в Java Servlet / JSP.Я пытаюсь построить web chat, используя Servlet и socket.io (javascript) с моей командой.

У меня есть динамический веб-проект, встроенный в Eclipse IDE.Все хорошо, прежде чем я добавлю что-то, связанное с socket.io, в свой проект.Хм, в частности, он включает в себя 2 js-файла: chat.js и index.js (и другие (index.html, package.json, ...)).

К сожалению, когда я запускаю свой проект с использованием веб-контейнера Tomcat, возникает ошибка: Get http:localhost:8080/socket.io..... not found (404).Итак, кто-нибудь может мне помочь ??

Ниже мой index.js

/*
 *  (C) 2018, All rights reserved. This software constitutes the trade secrets and confidential and proprietary information
 *  It is intended solely for use by Sandip Salunke. This code may not be copied or redistributed to third parties without 
 *  prior written authorization from Sandip Salunke
 */
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var onlineUsers = [];

// Initialize appication with route / (that means root of the application)
app.get('/', function(req, res) {
  var express = require('express');
  app.use(express.static(path.join(__dirname)));
  res.sendFile(path.join(__dirname, 'index.html'));
});

// Register events on socket connection
io.on('connection', function(socket) {

  // Listen to chantMessage event sent by client and emit a chatMessage to the client
  socket.on('chatMessage', function(message) {
    io.to(message.receiver).emit('chatMessage', message);
  });

  // Listen to notifyTyping event sent by client and emit a notifyTyping to the client
  socket.on('notifyTyping', function(sender, receiver) {
    io.to(receiver.id).emit('notifyTyping', sender, receiver);
  });

  // Listen to newUser event sent by client and emit a newUser to the client with new list of online users
  socket.on('newUser', function(user) {
    var newUser = {
      id: socket.id,
      name: user
    };
    onlineUsers.push(newUser);
    console.log(newUser.name);
    io.to(socket.id).emit('newUser', newUser);
    io.emit('onlineUsers', onlineUsers);
  });

  // Listen to disconnect event sent by client and emit userIsDisconnected and onlineUsers (with new list of online users) to the client 
  socket.on('disconnect', function() {
    onlineUsers.forEach(function(user, index) {
      if (user.id === socket.id) {
        onlineUsers.splice(index, 1);
        io.emit('userIsDisconnected', socket.id);
        io.emit('onlineUsers', onlineUsers);
      }
    });
  });

  socket.on('newGroup', function(newGroup) {
    newGroup.members.forEach(function(member) {
      onlineUsers.forEach(function(user) {
        if (user.name == member.name)
          member.id = user.id;
      })
    });
    newGroup.members.forEach(function(member) {
      io.to(member.id).emit('newGroup', newGroup.name);
    });
  });


  socket.on('acceptGroup', function(name) {
    socket.join(name);
  })

});

// Listen application request on port 3000
http.listen(3000, function() {
  console.log('listening on *:3000');
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...