未加星标

WebSockets or an alternative with phonegap?

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

How can I send low latency data to a server and back with phonegap?

Considering I don't have access tophp files locally, and don't have experience with node.js or WebSockets I don't know which ones I should use.

Problem courtesy of: maxhud

Solution

WebSockets aren't natively supported by the browsers in Android or older versions of Cordova under iOS , which means you'll need to use a PhoneGap plugin if you want to use them on the client.

There's more information at: http://remysharp.com/2010/10/04/websockets-in-phonegap-projects/

However, I'm not sure (even with the plugin) how resilient WebSockets are likely to be when the device moves between network connections (WiFi -> 3G -> WiFi), so using a simple polling web service may be a more reliable option if your app needs to continue receiving data as your users move around.

If you need to receive data initiated by the server, consider using push notifications instead: both iOS (APN) and Android (C2DM) provide APIs to do this which make more efficient use of the battery than having your app poll your server constantly.

Solution courtesy of: rmc47

Discussion

Refer to this link to see WebSocket Support by Browsers and devices : html5test.com site - iOS 4.2+ already supports WebSocket

See this doc that explains how to develop a simple application with WebSocket.

Unfortunately the content is in the Portuguese (Brazilian) language but you can leave comments which I will answer.

Discussion courtesy of: Joo Paraná

You can use WebSockets in PhoneGap with iOS and Android. WebSockets are natively supported on iOS within Safari. For Android you will need to use a polyfill.

See: https://stackoverflow.com/a/13527585/39904

The answer above provides information about how to make a WebSocket object available within the Android WebView used by PhoneGap and also provides a link to a sample project which you can use to get started.

WebSockets were developed as a solution to 'Comet' hacks. As such they provide a very low latency solution for realtime bi-directional communication between a client and server. This means low bandwidth and low resource usage - battery on mobile - since you are holding a single connection open rather then opening and closing multiple HTTP connections. A polling solution which makes requests at regular intervals is likely to drain the battery much faster than a WebSocket solution. If you are polling at lower intervals then it may be fine - it depends on your use case.

In terms of WebSockets working as you change between network and network type (WiFi -> 3G -> WiFi) then if you are using WebSockets natively you need to detect the onclose and reconnect. You will also need to determine the best type of connection; unsecure (WS) or secure (WSS). I would highly recommend you use WSS for mobile since some mobile network providers use transparent proxies which interfere with WS connections. This might sound complicated but there are a number of libraries that handle this for you. Such as the Pusher javascript library ( note: I work for Pusher ). Libraries such as these also provide fallback to a less efficient HTTP-based solution when the environment will not let any WebSocket connection occur.

Also see: realtime web technology guide .

I'd agree with @rmc47 that you should consider native push notifications if it's for infrequent single notifications

Discussion courtesy of: leggetter

I'm not sure what you mean by "access to PHP files locally". The use of node.js and web sockets is also not mutually exclusive.

If you have not made a decision on the server implementation you could go for either node.js or ASP.NET.

node.jshas good support for sockets with Socket.io , which abstracts the client implementation for you. So it will use WebSockets if there is support, else it will fall back to long polling.

ASP.NET has a library called SignalR which does something similar for the .NET platform.

Discussion courtesy of: codemonkey

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:WebSockets or an alternative with phonegap?
本站链接:https://www.codesec.net/view/611201.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(58)