最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keep WebSocket alive in Mobile Safari - Stack Overflow

programmeradmin5浏览0评论

Is it possible to keep a html 5 web pages WebSocket connection open in Mobile-Safari once the screen is locked?

I want to send my users continuous updates throughout the day and it seems silly that their screens should always have to be unlocked to receive those notifications.

Are there any other options?

Is it possible to keep a html 5 web pages WebSocket connection open in Mobile-Safari once the screen is locked?

I want to send my users continuous updates throughout the day and it seems silly that their screens should always have to be unlocked to receive those notifications.

Are there any other options?

Share Improve this question edited Aug 4, 2014 at 13:06 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Jul 17, 2014 at 6:13 TrevTheDevTrevTheDev 2,7372 gold badges25 silver badges41 bronze badges 3
  • what is the server side architecture are you using? – khagesh Commented Aug 2, 2014 at 9:39
  • @Pinal: please don't add inline code spans (like this) for emphasis. See this Meta post for more information. – Qantas 94 Heavy Commented Aug 4, 2014 at 13:06
  • @khagesh: I'm not sure server architecture matters? I'm using rails 4 with the rails-websocket gem. – TrevTheDev Commented Aug 5, 2014 at 2:29
Add a ment  | 

2 Answers 2

Reset to default 9 +50

I don't think it's possible to keep the connection open while the browser is in the background, or when the screen is locked, the reason being that the app is essentially frozen in memory. Here's a quote from a similar question:

the reason you cant keep a network socket open, is that without your app jumping to the foreground when it receives a connection, it cannot respond to network traffic(because if it is not in the foreground its memory content is frozen).

However, I did find this page on Push Notifications for Websites that shows you 'how to sign up your users to receive notifications even when your site is not running in Safari'.

There are some other options: if you want to send continuous updates, you could write an app and either follow the instructions on Apple's site to keep a socket open permanently, or you could configure the app to implement Push Notifications.

I'm sorry I couldn't find a quick fix, but I hope at least one of these options works for you!

I have found a hacky way to keep WebSocket alive in Mobile Safari.

Basically it's the same solution as for this question.

Create an infinity looping audio file to keep Javascript running:

<audio loop src="http://www.sousound./music/healing/healing_01.mp3"></audio>

Note: some user interaction is required to initiate the audio file.

It would be nice if a WebSocket kept the browser alive in the same manner as an audio or video file.

PS this also works on Android.

发布评论

评论列表(0)

  1. 暂无评论