{"id":21037466,"url":"https://github.com/vintasoftware/trigger.io-tcp","last_synced_at":"2025-03-13T20:42:42.402Z","repository":{"id":13232078,"uuid":"15916643","full_name":"vintasoftware/trigger.io-tcp","owner":"vintasoftware","description":"trigger.io module for TCP sockets","archived":false,"fork":false,"pushed_at":"2014-01-24T21:59:29.000Z","size":868,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-01-20T16:22:13.775Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vintasoftware.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-14T21:48:14.000Z","updated_at":"2018-11-12T05:22:25.000Z","dependencies_parsed_at":"2022-09-19T02:50:36.005Z","dependency_job_id":null,"html_url":"https://github.com/vintasoftware/trigger.io-tcp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vintasoftware%2Ftrigger.io-tcp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vintasoftware%2Ftrigger.io-tcp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vintasoftware%2Ftrigger.io-tcp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vintasoftware%2Ftrigger.io-tcp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vintasoftware","download_url":"https://codeload.github.com/vintasoftware/trigger.io-tcp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243482877,"owners_count":20297897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T13:26:25.763Z","updated_at":"2025-03-13T20:42:42.380Z","avatar_url":"https://github.com/vintasoftware.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# trigger.io-tcp\n\n## Overview\ntrigger.io module for TCP sockets (android-only for now)\n\n## Usage\n**tcp** module has an object-oriented interface for creating TCP Sockets. You can create a socket with `var socket = new forge.tcp.Socket(ip, port)`, connect it with `socket.connect()` and send data with `socket.send('hello world')`. You don't need to pass a callback on each method call, since calls will be ordered in a async-pipeline fashion. For optimization purposes, there is a JS side buffer. When you call `flush` or `close`, data will be sent from JS to native and then to network. To read data from a socket is necessary to pass a callback to get received data, like `socket.read(callback)`\n\n### Example\n```javascript\nvar socket = new forge.tcp.Socket('10.0.2.2', 7070);\nsocket.connect();\nsocket.send('hello world');\nsocket.flush();\nsocket.read(function (data) {\n    console.log('received: ' + data);\n    socket.close();\n});\n```\n\n## Object-oriented API\n### creating a socket\nUse the `forge.tcp.Socket(ip, port, config)` constructor. `ip` must be a string and `port` an integer. `config` is optional. Don't forget the `new` keyword. Also, don't forget to `connect` it afterwards:\n```javascript\nvar socket = new forge.tcp.Socket('10.0.2.2', 7);\n```\nTo customize the socket, use the `config` argument. `config` is a object that supports the following properties and defaults:  \n\n* `config.charset = 'UTF-8'` Java [Charset](http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html) in which data will be encoded/decoded when the socket send/read data. This has nothing to do with JS or Java internal string encoding. This charset is needed because data will be sent/read as bytes in the native socket\n* `config.connectionTimeout = 30000` connection timeout in ms\n* `config.maxBufferLength = 65536` JS buffer length in JS chars. Data is buffered in JS side to avoid unnecessary repeated calls to the resource-intensive native bridge. Use `flush` method when you need to send data to native side without filling the buffer\n\nThe `config` also support the following socket events:\n* `config.onConnect` function to be called immediately after the socket is connected\n* `config.onClose` function to be called immediately after the socket is closed\n* `config.onError` function to be called immediately after an error occurs. When an error occurs, all subsequent calls to socket methods will not be executed, i.e., an error interrupts a socket method pipeline\n\n### socket methods\nWhen you call a socket method, this call will be queued to the socket pipeline. This means you can call `send`, `flush`, `close` without using callbacks. The call will return immediately, but will be executed when the previous operation is completed, meaning that call order is preserved. Only `read` method needs a callback to get received data. See the [example in Usage](#example) section above\n\n#### connecting\nUse the `socket.connect()` to connect a socket to the server\n\n#### sending data\nUse the `socket.send(data)` method. `data` must be a string and will be encoded as `config.charset` bytes before is sent to network:\n```javascript\nvar data = 'hello world';\nsocket.send(data);\n```\n\n#### flushing data\nData is buffered both in JS and native sides, so when you are developing for a send/read protocol you may need to flush data beforing reading:\n```javascript\nsocket.flush();\n```\n\n#### reading data\nUse the `socket.read(function (data) { ... })` method. `data` is the received data as string, decoded from bytes with `config.charset`:\n```javascript\nsocket.read(function (data) {\n    // do something with data...\n    // call more socket methods...\n});\n```\n\n#### closing the socket\nUse the `socket.close()` method when you are done with it","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvintasoftware%2Ftrigger.io-tcp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvintasoftware%2Ftrigger.io-tcp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvintasoftware%2Ftrigger.io-tcp/lists"}