Java row websocket data transfer - javascript

So basically i have java ServerSocket connected with browser which has js websocket on it. After correct handshaking i'm reciving data in this cycle
while((d = in.read()) != -1) {
System.out.println(Integer.toString(d));
}
Client sends data with
socket.send(send_data.value);
Where send_data is an input element;
And when i send data repeatedly i always get different bytes, and what is most important - number of bytes is not divisible by 4.
Why is this happening? Should i use some ByteBuffer stuff or flush()?

Related

VB equivalent of Node-Red Buffer.from?

I'll try to summarize the issue the best way I can. I recently purchased a dual interface board that has TCP communication capabilities. In order to get information from the board an array of bytes needs sent to the board in which it will respond with the desired information. In node red I have been able to send the array of bytes using a function and receive a response from the TCP module.
However I would like to use this in an application I am developing that is more user-friendly than node red. Unfortunately, no matter what I have tried I have not been able to receive a response from the device using Visual Basic.
In node red, the array looks something like this:
Msg.payload = Buffer.from([8,121,50,3,100)];
Without buffer.from the device would not respond. I have tried encoding the string in VB into a byte variable and sending via socket, but am having no luck. Any suggestions? Here is the VB code.
Imports System.Net
Public Class Form1
Dim TCPClientz As Sockets.TcpClient
Dim TCPClientStream As Sockets.NetworkStream
Private Sub SendBytesButton_Click(sender As Object, e As EventArgs) Handles SendBytesButton.Click
'Dim intcount As Integer
Dim sendbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(bytestextbox.Text)
TCPClientz = New Sockets.TcpClient(ServerTextBox.Text, PortTextBox.Text) 'Device IP and Port are working. Changing port throws error.
TCPClientStream = TCPClientz.GetStream()
'intcount = TCPClientz.Client.Send(sendbytes)
TCPClientStream.Write(sendbytes, 0, sendbytes.Length)
If TCPClientStream.DataAvailable = True Then 'At this point, I NEVER have gotten the stream to indicate there is available data.
Dim rcvbytes(TCPClientz.ReceiveBufferSize) As Byte
TCPClientStream.Read(rcvbytes, 0, CInt(TCPClientz.ReceiveBufferSize))
replytextbox.Text = System.Text.Encoding.UTF8.GetString(rcvbytes)
End If
End Sub
Does anyone know of any way to capture the bytes being sent by NODE-RED? I can view the payload, but I don't believe this is a representation of the actual bytes being sent. I could try to pair this up with the BYTE array in VB to see if they match.

Why my byte array size increase on the client side

I send a byte array from my web server to my client :
byte[myByteArray] = null;
// filling myByteArray
return Json(myByteArray, JsonRequestBehavior.AllowGet);
and myByteArray contains 480000 bytes so basically it should be around 480KB
but when I send it to my client and I watch the Network section on chrome console, the response size is over 1.5MB
Why ? My first answer that comes to my mind is the serialization , if it is the case , how can I send a byteArray without Serializing it, as file maybe ?

Uploading/Downloading Byte Arrays with AngularJS and ASP.NET Web API

I have spent several days researching and working on a solution for uploading/downloading byte[]’s. I am close, but have one remaining issue that appears to be in my AngularJS code block.
There is a similar question on SO, but it has no responses. See https://stackoverflow.com/questions/23849665/web-api-accept-and-post-byte-array
Here is some background information to set the context before I state my problem.
I am attempting to create a general purpose client/server interface to upload and download byte[]’s, which are used as part of a proprietary server database.
I am using TypeScript, AngularJS, JavaScript, and Bootstrap CSS on the client to create a single page app (SPA).
I am using ASP.NET Web API/C# on the server.
The SPA is being developed to replace an existing product that was developed in Silverlight so it is constrained to existing system requirements. The SPA also needs to target a broad range of devices (mobile to desktop) and major OSs.
With the help of several online resources (listed below), I have gotten most of my code working. I am using an asynchronous multimedia formatter for byte[]’s from the Byte Rot link below.
http://byterot.blogspot.com/2012/04/aspnet-web-api-series-part-5.html
Returning binary file from controller in ASP.NET Web API
I am using a jpeg converted to a Uint8Array as my test case on the client.
The actual system byte arrays will contain mixed content compacted into predefined data packets. However, I need to be able to handle any valid byte array so an image is a valid test case.
The data is transmitted to the server correctly using the client and server code shown below AND the Byte Rot Formatter (NOT shown but available on their website).
I have verified that the jpeg is received properly on the server as a byte[] along with the string parameter metadata.
I have used Fiddler to verify that the correct response is sent back to the client.
The size is correct
The image is viewable in Fiddler.
My problem is that the server response in the Angular client code shown below is not correct.
By incorrect, I mean the wrong size (~10K versus ~27.5K) and it is not recognized as a valid value for the UintArray constructor. Visual Studio shows JFIF when I place the cursor over the returned “response” shown in the client code below, but there is no other visible indicator of the content.
/********************** Server Code ************************/
Added missing item to code after [FromBody]byte[]
public class ItemUploadController : ApiController{
[AcceptVerbs("Post")]
public HttpResponseMessage Upload(string var1, string var2, [FromBody]byte[] item){
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(item);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
}
/***************** Example Client Code ********************/
The only thing that I have omitted from the code are the actual variable parameters.
$http({
url: 'api/ItemUpload/Upload',
method: 'POST',
headers: { 'Content-Type': 'application/octet-stream' },// Added per Byte Rot blog...
params: {
// Other params here, including string metadata about uploads
var1: var1,
var2: var2
},
data: new Uint8Array(item),
// arrybuffer must be lowecase. Once changed, it fixed my problem.
responseType: 'arraybuffer',// Added per http://www.html5rocks.com/en/tutorials/file/xhr2/
transformRequest: [],
})
.success((response, status) => {
if (status === 200) {
// The response variable length is about 10K, whereas the correct Fiddler size is ~27.5K.
// The error that I receive here is that the constructor argument in invalid.
// My guess is that I am doing something incorrectly with the AngularJS code, but I
// have implemented everything that I have read about. Any thoughts???
var unsigned8Int = new Uint8Array(response);
// For the test case, I want to convert the byte array back to a base64 encoded string
// before verifying with the original source that was used to generate the byte[] upload.
var b64Encoded = btoa(String.fromCharCode.apply(null, unsigned8Int));
callback(b64Encoded);
}
})
.error((data, status) => {
console.log('[ERROR] Status Code:' + status);
});
/****************************************************************/
Any help or suggestions would be greatly appreciated.
Thanks...
Edited to include more diagnostic data
First, I used the angular.isArray function to determine that the response value is NOT an array, which I think it should be.
Second, I used the following code to interrogate the response, which appears to be an invisible string. The leading characters do not seem to correspond to any valid sequence in the image byte array code.
var buffer = new ArrayBuffer(response.length);
var data = new Uint8Array(buffer);
var len = data.length, i;
for (i = 0; i < len; i++) {
data[i] = response[i].charCodeAt(0);
}
Experiment Results
I ran an experiment by creating byte array values from 0 - 255 on the server, which I downloaded. The AngularJS client received the first 128 bytes correctly (i.e., 0,1,...,126,127), but the remaining values were 65535 in Internet Explorer 11, and 65533 in Chrome and Firefox. Fiddler shows that 256 values were sent over the network, but there are only 217 characters received in the AngularJS client code. If I only use 0-127 as the server values, everything seems to work. I have no idea what can cause this, but the client response seems more in line with signed bytes, which I do not think is possible.
Fiddler Hex data from the server shows 256 bytes with the values ranging from 00,01,...,EF,FF, which is correct. As I mentioned earlier, I can return an image and view it properly in Fiddler, so the Web API server interface works for both POST and GET.
I am trying vanilla XMLHttpRequest to see I can get that working outside of the AngularJS environment.
XMLHttpRequest Testing Update
I have been able to confirm that vanilla XMLHttpRequest works with the server for the GET and is able to return the correct byte codes and the test image.
The good news is that I can hack around AngularJS to get my system working, but the bad news is that I do not like doing this. I would prefer to stay with Angular for all my client-side server communication.
I am going to open up a separate issue on Stack Overflow that only deals with the GET byte[] issues that I am have with AngularJS. If I can get a resolution, I will update this issue with the solution for historical purposes to help others.
Update
Eric Eslinger on Google Groups sent me a small code segment highlighting that responseType should be "arraybuffer", all lower case. I updated the code block above to show the lowercase value and added a note.
Thanks...
I finally received a response from Eric Eslinger on Google Group. He pointed out that he uses
$http.get('http://example.com/bindata.jpg', {responseType: 'arraybuffer'}).
He mentioned that the camelcase was probably significant, which it is. Changed one character and the entire flow is working now.
All credit goes to Eric Eslinger.

Dealing with GET char limit

I have a .js script that sends data to a .py script running on the local host.
To send data from the .js, I have the following work around (to deal with the limitations of XmlHTTPRequest):
var req = document.createElement("img");
req.src = "http://0.0.0.0:8000?var="+data
To recieve it on the python end:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
data = conn.recv(1024)
conn.close()
print data
This works fine, but my problem is that the char limit on a GET prevents me from sending all of the data I need. I tried making to URI GET requests (using the first block of code twice), but my python only recieved the first request. How can I send/recieve mulitple GET requests? I am assuming I will need some sort of loop, but am unsure what steps need to be in the loop.
From your Python code, it doesn't seem like the HTTP request method used makes any difference. In that case, I'd recommend setting up a form element with method="post" and just putting whatever you want in it. There's no theoretical limit on post body length.

How to extract messages from the dashboard in Mirth?

How to extract messages from the dashboard in Mirth?
Basically using java script, how would I extract the information from dashboard in Mirth.
For example, I am after extracting the encoded data and ACK back from the destination.
One of the thing I tried was to run the following the postprocessor. But it’s only writing raw message not the encoded.
var log1file=D:\TEST\log1.txt;
var ReportBody=(messageObject.getEncodedData());
FileUtil.write(log1file, true, ReportBody);
Any suggestions much appreciated.
Thank you.
try this...
logger.info('start post script');
var status = responseMap.get('Destination Name').getStatus();
if ((status == "ERROR" || status == "FAILURE") )
{
logger.info("Status = "+status);
var errormsg = responseMap.get('Destination Name').getMessage();
logger.info(errormsg);
}
return;
getMessage() describe exception(error) description.
You wouldn't want to extract messages from the Dashboard. The dashboard is only showing the stored history from the database it keeps.
If you want to write the encoded data to a log file as the messages are processed, move that code from your post-processor over to a transformer javascript step in the source or in a destination (the encoded data changes from source to destination if you have transformer steps or if you change from HL7 to XML, etc.)
Is it actually creating the file? You don't have quotes around your file name and the backslashes should be forward slashes.

Categories