项目作者: Yoonit-Labs

项目描述 :
Build modern apps using NativeScript and WebSocket in Android and iOS
高级语言: JavaScript
项目地址: git://github.com/Yoonit-Labs/nativescript-yoonit-websocket.git
创建时间: 2020-10-07T20:42:10Z
项目社区:https://github.com/Yoonit-Labs/nativescript-yoonit-websocket

开源协议:MIT License

下载


NativeScript Yoonit Camera









Build modern apps using NativeScript and WebSocket in Android and iOS

  • Stable Websocket connection
  • Modern JS Code (ESNext)
  • Works in emulator and real devices
  • Timeout configuration
  • Automatic reconnection
  • Proxy options
  • Custom headers
  • VueJS Plugin

Sponsors












Platinum



Become a sponsor!

Installation

  1. npm i -s @yoonit/nativescript-websocket

Usage

All the functionalities that the @yoonit/nativescript-websocket provides is accessed through the YoonitWebSocket object. Below we have the basic usage code, for more details, your can see the Methods or the Demo Vue.

VueJS Plugin

main.js

  1. import Vue from 'nativescript-vue'
  2. import YoonitWebSocket from '@yoonit/nativescript-websocket/vue'
  3. Vue.use(
  4. YoonitWebSocket,
  5. 'wss://echo.websocket.org/',
  6. {
  7. protocol: '',
  8. timeout: 1000,
  9. headers: [],
  10. reconnect: true,
  11. delay: 1000,
  12. debug: false,
  13. proxy: {
  14. address: '',
  15. port: ''
  16. }
  17. }
  18. )

After that, you can access the socket object in your entire project using this.$yoo.socket

Angular, React, Svelte or any other framework

Currently we can’t offer any other integration with other frameworks that works on top of NativeScript beyond VueJS, you are totaly open to create and send to us a PR. But, this is a pure NativeScript plugin, if you know how to manipulate your preferred platform you will be capable to include it in your project.

Vue Component

Declaring events using an Yoonit-Style

App.vue

  1. <template>
  2. <Page @loaded="doLoaded"></Page>
  3. </template>
  4. <script>
  5. export default {
  6. data: () => ({
  7. interval: null
  8. }),
  9. methods: {
  10. doLoaded () {
  11. // start the connection
  12. this.$yoo.socket.open()
  13. },
  14. doPing () {
  15. this.interval = setInterval(() => {
  16. if (!this.$yoo.socket.getStatus()) {
  17. return console.log('[YooSocket] Socket closed')
  18. }
  19. console.log("[YooSocket] Sending 'echo' message!")
  20. // add your message/file to queue and call 'send' method
  21. return this.$yoo.socket.push('echo')
  22. }, 2000)
  23. }
  24. },
  25. yoo: {
  26. socket: {
  27. events: {
  28. open ($socket) {
  29. console.log("[YooSocket] Hey! I'm connected!")
  30. clearInterval(this.interval)
  31. return this.doPing()
  32. },
  33. message ($socket, message) {
  34. if (!message) {
  35. console.log("[YooSocket] Message is empty")
  36. }
  37. console.log(`[YooSocket] Received Message: '${message}'!`)
  38. },
  39. close () {
  40. console.log("[YooSocket] Socket was closed")
  41. },
  42. error () {
  43. console.log("[YooSocket] Socket had an error")
  44. }
  45. }
  46. }
  47. }
  48. }
  49. </script>

Or declaring events using your own already created methods

App.vue

  1. <template>
  2. <Page @loaded="doLoaded"></Page>
  3. </template>
  4. <script>
  5. export default {
  6. data: () => ({
  7. interval: null
  8. }),
  9. methods: {
  10. doLoaded () {
  11. // start the connection
  12. this.$yoo.socket.open()
  13. // declare all callback events
  14. this.$yoo.socket.events({
  15. open: this.doSocketOpen,
  16. message: this.doReceivedMessage,
  17. close: this.doSocketClose,
  18. error: this.doSocketError
  19. })
  20. },
  21. doPing () {
  22. this.interval = setInterval(() => {
  23. if (!this.$yoo.socket.getStatus()) {
  24. return console.log('[YooSocket] Socket closed')
  25. }
  26. console.log("[YooSocket] Sending 'echo' message!")
  27. // add your message/file to queue and call 'send' method
  28. return this.$yoo.socket.push('echo')
  29. }, 2000)
  30. },
  31. doSocketOpen ($socket) {
  32. console.log("[YooSocket] Hey! I'm connected!")
  33. clearInterval(this.interval)
  34. // onOpen event calls your function
  35. return this.doPing()
  36. },
  37. doSocketClose () {
  38. // onClose event
  39. return console.log('[YooSocket] Socket was closed')
  40. },
  41. doSocketError () {
  42. // onError event
  43. return console.log('[YooSocket] Socket had an error')
  44. },
  45. doReceivedMessage ($socket, message) {
  46. // onMessage event
  47. return console.log(`[YooSocket] Received Message: '${message}'!`)
  48. },
  49. }
  50. }
  51. </script>

API

Methods

Function Parameters Valid values Return Type Description
openAsync timeout any positive number, default 1000 (ms) void Wait timeout to start the connection
open - - void Start immediately the connection
on event, callback string, function void Include an event, every that your server sent an event of this type, your callback will be invoked
off event, callback string, function void Exclude an event
events callbacks object with functions { open: [], close: [], message: [], error: [], fragment: [], handshake: [] } void You can use an Array of callbacks in each event. Use this to add a batch of events at once
getStatus - - boolean You can use this method to check the connection status
push content string/blob void Send files or strings to server. You can use this method to make uploads for example
destroy - - void Destroy server connection
close code, message number, string void Close server connection programmatically offering an reason
queueLength - - number Get the total pending items to be sent
isOpen - - boolean Get connection status
isClosed - - boolean Get connection status
isClosing - - boolean Get connection status
isConnecting - - boolean Get connection status

Properties

Property Return Type
protocol string
timeout number
headers array
reconnect boolean
delay number
debug boolean
proxy object
callbacks object
url string
opened boolean

To contribute and make it better

Clone the repo, change what you want and send PR.
For commit messages we use Conventional Commits.

Contributions are always welcome!




Code with ❤ by the Yoonit Team