Skip to content

continue send http request with transport=polling #5

@aximo

Description

@aximo

does it support socket.io?
when i use socket.io js, it will sent many http request with params such as http://localhost:7070/commonstore-service/gs-guide-websocket/?EIO=3&transport=polling&t=1546595407877-10 (transport is alway polling) and response with 200. then java backend will use up threads.

i also notice there is a project for socket io https://github.com/trinopoty/socket.io-server-java, and i use it ,but the question appears too.

how about to process issue about sent http request continuously?

the html is

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
      #messages { margin-bottom: 40px }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      $(function () {
        var socket = io('http://localhost:7070',{
		  path: '/commonstore-service/gs-guide-websocket/',
		  reconnection: false
		});
        $('form').submit(function(){
          socket.emit('chat message', $('#m').val());
		  $('#messages').append($('<li>').text("send:" + $('#m').val()));
          $('#m').val('');
          return false;
        });
        socket.on('chat message', function(msg){
          $('#messages').append($('<li>').text("receive:" + msg));
          window.scrollTo(0, document.body.scrollHeight);
        });
		
		socket.on('test', function(msg){
          $('#messages').append($('<li>').text("receive:" + msg));
          window.scrollTo(0, document.body.scrollHeight);
        });
		
		socket.on('disconnect', (reason) => {
		  console.log("disconnect for " + reason)
		  $('#messages').append($('<li>').text("disconnect:" + msg));
          window.scrollTo(0, document.body.scrollHeight);
		  if (reason === 'io server disconnect') {
			// the disconnection was initiated by the server, you need to reconnect manually
			socket.connect();
		  }
		  // else the socket will automatically try to reconnect
		});
		
		socket.on('connect', () => {
		  $('#messages').append($('<li>').text("connect:" + msg));
		});
		
		socket.on('error', function(msg){
          $('#messages').append($('<li>').text("error:" + msg));
          window.scrollTo(0, document.body.scrollHeight);
        });
      });
    </script>
  </body>
</html>

java server:

private ConcurrentMap<String,EngineIoSocket> cache=new ConcurrentHashMap<>();
	
	@Bean
	public EngineIoServer socketServer() {
		EngineIoServer engineServer=new EngineIoServer();
		
		engineServer.on("connection", new Emitter.Listener() {
			@Override
			public void call(Object... args) {
				EngineIoSocket socket = (EngineIoSocket) args[0];
				addSocket(socket);
				logger.info("success connection {}",socket.getId());
			}
		});
		
		//new SocketIoServer(engineServer);
		return engineServer;
	}

	protected void addSocket(EngineIoSocket socket) {
		socket.on("message", new Emitter.Listener() {
		    @Override
		    public void call(Object... args) {
		       Object message = args[0];
		       logger.info("success receive message... {}",message);
		    }
		});
		cache.putIfAbsent(UUIDUtils.uuid16(),socket);
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions