Hello, In my application I need to use a custom protocol over raw TCP sockets (so socket.io is out of scope). I need to connect the mobile device to another with a certain IP address, and send and receive data over this connection. As far as I have seen, the closes plugin that could support this for ionic 2 would be cordova-plugin-chrome-apps-sockets-tcp, but I have not had any luck in using this in an ionic 2 project.
I have tried both declare var chrome;
and (<any>window).plugins.chrome.tcp.socket.create();
without any success.
For anyone interested: the reference to the plugin is made via window, but not through the plugins namespace, but directly. Thus, in order to call, for instance, the create method, one should use: (<any>window).chrome.tcp.socket.create();
Still canāt understand how to use this plugin in an Ionic 2 provider. With other plugins I just import them and they are available for use. Anyone can help?
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the TCPServices provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class TCPServices {
constructor(public http: Http) {
console.log('Hello TCPServices Provider');
}
arrayBuffer2str(buf) {
var str= '';
var ui8= new Uint8Array(buf);
for (var i= 0 ; i < ui8.length ; i++) {
str= str+String.fromCharCode(ui8[i]);
}
return str;
}
str2arrayBuffer(str) {
var buf= new ArrayBuffer(str.length);
var bufView= new Uint8Array(buf);
for (var i= 0 ; i < str.length ; i++) {
bufView[i]= str.charCodeAt(i);
}
return buf;
}
sendPacket(ipAddr,ipPort,data) {
var delay= 5000; /// 5 seconds timeout
(<any>window).chrome.sockets.tcp.create({}, createInfo => { //callback function with createInfo as the parameter
var _socketTcpId= createInfo.socketId;
(<any>window).chrome.sockets.tcp.connect(_socketTcpId, ipAddr, ipPort, result => { //callback function with result as the parameter
if (result === 0) {
var data2send= this.str2arrayBuffer(data);
/// connection ok, send the packet
(<any>window).chrome.sockets.tcp.send(_socketTcpId, data2send);
}
});
(<any>window).chrome.sockets.tcp.onReceive.addListener( info => { //callback function with info as the parameter
/// recived, then close connection
(<any>window).chrome.sockets.tcp.close(_socketTcpId);
var data= this.arrayBuffer2str(info.data);
});
/// set the timeout
setTimeout(function() {
(<any>window).chrome.sockets.tcp.close(_socketTcpId);
}, delay);
});
}
}
hi from france,
i start ionic2 personnal project and i want to use ethernet thermal printer, but i have error :
chrome.sockets.tcp.send - data is not an Array Buffer! (Got: String)
can you help and explain me how to send esc-pos character to printer,
Indeed, you need to send an arraybuffer. So, please note that in the sendPacket function within the code attached to the thread, there is a str2arrayBuffer function that does exactly this conversion.
var amountReceived = 0;
function recvListener(info)
{
amountReceived += info.data.byteLength;
var arr = new Uint8Array(info.data);
var textChunk = String.fromCharCode.apply(null,arr);
}
note that for large messages you might have multiple receives to get the whole message