weixin_33690963 2015-07-23 23:00 采纳率: 0%
浏览 85

Websocket还是AJAX或两者兼而有之?

I am a bit confused about how one uses websockets. I have already set up a websocket server and are able to receive from server and send to server.

My question is. When using websockets, are you supposed to drop the ajax part completely? Or are you supposed to use websockets alongside ajax?

Example:

I want to use websockets on a chat service on a website where users can log in. When logging in, I use ajax. When on the message page I use websockets to receive future messages, and send future messages. But when navigating to the messages page, I use ajax to get the messages from database.

Is this the correct way of using websockets? Or should I do everything in websockets since 1 user 1 active connection is more efficient? Or should 1 user have 1 websocket connection, but still keep sending ajax requests to the server when navigating to some pages without reloading the site, using ajax?

What is the best practice when creating a large application where users can log in, navigate to pages to load stuff async, but still wanting that bidirectional real time benefits of websockets on stuff like messaging services and notification services on that same website?

  • 写回答

1条回答 默认 最新

  • weixin_33724570 2015-07-23 23:03
    关注

    There is no reason you cannot use both. I think the solution depends on what server side resources you are using. I may need access to a server through websockets to get updates to a chat dialogue. I may use a completely different system to get user statistics or provide authentication.

    I don't think there is a specific answer to your question as it varies depending on the application in question.

    评论

报告相同问题?