Reputation: 21
I need to develop web application. This application will implement web-based terminal for some application server (this project will provide alternative thin and cross-platform web-based client instead of windows-only binary client). I understand than using ajax for communicating with app.server is not correct, because we need to be more real-time. So we need something more asynchronous than ajax. I planned to use web sockets.
But my application need to communicate with remote application server too. Existing implementation is based on TCP. So we have to use this existing protocol specification. I can imagine how to connect to remote server when new web-socket connection is accepted. And I know web-socket implementation is easy because it's event driven. However I know classic java socket is stream based, and I have to read from stream and my code will be blocked until some data appears from remote side. So code will not be able to respond to another events.
I see one way to make it correct - use threads for classic sockets. But I don't know if it's correct for web java application. Please suggest me any way.
Upvotes: 0
Views: 1294
Reputation: 69703
There are various ways for Java to communicate asynchronously so that different connections do not block each other. The current standard is Java's Non-blocking IO framework which abstracts each network connection in form of a Channel and groups them under a Selector which finds out which channels are currently ready to receive data or which just received data which can be retrieved by your code.
The framework handles the actual IO with separate background threads, so writing to and reading from a socket are almost instantly.
Upvotes: 0