Reputation: 37098
EDIT: This question can be summed up as follows: How can two separate html pages, in separate windows, exchange information asynchronously?
For a class project, I have to build a web-based chat client. The professor provided a working server.
I have a portion of it up and running-- any connected users can send messages to the server, and it will be relayed to all connected users and appear in their main chat window, chatroom-style.
However, the client is additionally required to have the option of sending/receiving private messages. If a private message is sent or received, a new window is opened, showing a chat interface for communication only with that user. Any future private messages sent/received by the client to/from that user will appear in this window instead of the main chatroom.
I'm at a complete loss. Having received a private message, how can I open a new window and then continue to communicate with that window? If more private messages are received from that user through the main window's websocket connection, they must be sent to that window, and if messages are sent from that window to that other user, they must be relayed through the main window's websocket connection.
How can this be done, if it can be done at all?
Upvotes: 2
Views: 353
Reputation: 855
Use SignalR. It focuses on what you want to do. Happy coding! :)
See this link: Asynchronous scalable web applications with real-time persistent long-running connections with SignalR
http://www.youtube.com/watch?v=1wh33W-09bk
Upvotes: 0
Reputation: 19717
I'd tackle it like this:
Your page initially starts and uses setTimeOut type function to do an AJAX call to the server, checking for all "public" messages. When it wants to post a message, it does another AJAX call to send a public message.
I'd probably add yet another AJAX call to the server to asks "Any private messages for me?" Server could respond with "Yep, here's one from userid 1234". Then you could use window.open to open a window named "FromUserID1234" or something. Store that reference (using some of zzzzBow's answer). Using that reference, any future private messages that are received, you can check to see if there is already a Private Message window opened. If so, ignore it, if not, then create one. This will also allow you to open a Private Message window, use it for a while, close it, then if another one is received from the same user, it will just open another window.
The new window "FromUserID1234" then uses AJAX to communicate to the servers, asking "Do you have any more messages for me from userid 1234?". Server can respond with new ones, and also send messages.
Sorry, this isn't exact code, per se, but this is where I'd start defining the problem and then just build the communication interfaces to make it all happen.
Upvotes: 0
Reputation: 179196
window.open
returns the window
instance belonging to the newly opened window. If the address opened in the new window acceptably falls in the same origin policy, you can modify it and its DOM just like you'd be able to modify the currently active window.
var w,
someElement;
w = window.open('somepage.html', 'some window name', 'width=300,height=200');
someElement = w.document.getElementById('some-element-id');
Upvotes: 5