How to know end of a single event of Server-Sent Event? - spring

I have for example, the following server code:
emitter.send(SseEmitter.event().id("id").name("eventName").data(dummyData.getName()));
delay(10);
emitter.send(SseEmitter.event().id("id").name("eventBalance").data(dummyData.getBalance()));
delay(5000);
emitter.send(SseEmitter.event().id("id").name("eventHistory").data(dummyData.getHistory()));
emitter.complete();
When I receive it on client side (I use flutter, but I believe that does not matter), I get the events like this:
I/flutter (17221): id:id
I/flutter (17221): event:eventName
I/flutter (17221): data:Joe
I/flutter (17221):
I/flutter (17221): id:id
I/flutter (17221): event:eventBalance
I/flutter (17221): data:2511
I/flutter (17221):
I/flutter (17221): id:id
I/flutter (17221): event:eventHistory
I/flutter (17221): data:[{"dateTime":"2022-04-22T19:01:38.184845+01:00","value":"somevalue"}]
I/flutter (17221):
I/flutter (17221): done
I printed only the body here. As far as I can see, the only thing that signals an event is sent completely in the stream (all of this is in a single stream) is an emppy line. But what if the request had an empty line too?

I found that Server-Sent Events use double newline to separate events, there is no other way to know.
Source: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format

Related

Cannot get an image from API

i create an flutter application connected with (API), everything work good, and i can fetch all data from my API, but with image, i encounter a problem, in flutter i get the right URL for image from API, i try it and it is work good when i just paste the URL like this everything work good, and image displayed in mobile:
leading: Image.network("http://192.168.43.106:3000/cupload/0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png"),
this url (http://192.168.43.106:3000/cupload/0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png), i fetch it from API ..... by this code:
getCategories() async {
var res = await http.get("http://192.168.43.106:3000/cat");
if(res.statusCode == 200){
var jsonObject = json.decode(res.body);
print(jsonObject['result']);
return jsonObject['result'];
}
and then :
#override
void initState() {
getCategories();
super.initState();
}
#override
Widget build(BuildContext context) {
return Center(
child: FutureBuilder(
future: getCategories(),
builder: (context, snapshot){
if(snapshot.data != null){
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return ListTile(
leading: Image.network(snapshot.data[index]['img']),/*
}
but as you see when i make
leading: Image.network(snapshot.data[index]['img'])
nothing display in mobile and i see this message:
I/flutter (25006): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter (25006): The following NetworkImageLoadException was thrown resolving an image codec:
I/flutter (25006): HTTP request failed, statusCode: 404,
I/flutter (25006): http://192.168.43.106:3000/cupload%5C0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (25006):
I/flutter (25006): When the exception was thrown, this was the stack:
I/flutter (25006): #0 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:90:9)
I/flutter (25006): <asynchronous suspension>
I/flutter (25006): #1 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:47:14)
I/flutter (25006): #2 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
I/flutter (25006): #3 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
I/flutter (25006): #4 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:325:84)
when i add this code
print(snapshot.data[index]['img']);
inside (itemBuilder in the ListView) this is the result:
Restarted application in 1,428ms.
I/flutter (21905): http://192.168.43.106:3000/mo11111
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/upload\500_F_216231494_oJFKyTVOUoiFMzvhhetAcSPktyZwhp7L.jpg
I/flutter (21905): http://192.168.43.106:3000/cupload\Capture.PNG
I/flutter (21905): http://192.168.43.106:3000/cupload\1_ir8j2Ag89QZFAKBZDZWEUw.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\download (1).jfif
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/mo11111
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/upload\500_F_216231494_oJFKyTVOUoiFMzvhhetAcSPktyZwhp7L.jpg
I/flutter (21905): http://192.168.43.106:3000/cupload\Capture.PNG
I/flutter (21905): http://192.168.43.106:3000/cupload\1_ir8j2Ag89QZFAKBZDZWEUw.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\download (1).jfif
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following NetworkImageLoadException was thrown resolving an image codec:
HTTP request failed, statusCode: 404, http://192.168.43.106:3000/cupload%5C0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
When the exception was thrown, this was the stack:
#0 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:90:9)
<asynchronous suspension>
#1 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:47:14)
#2 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
#3 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
...
Image provider: NetworkImage("http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png", scale: 1.0)
Image key: NetworkImage("http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (21905): http://192.168.43.106:3000/mo11111
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m11
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/m1
I/flutter (21905): http://192.168.43.106:3000/upload\500_F_216231494_oJFKyTVOUoiFMzvhhetAcSPktyZwhp7L.jpg
I/flutter (21905): http://192.168.43.106:3000/cupload\Capture.PNG
I/flutter (21905): http://192.168.43.106:3000/cupload\1_ir8j2Ag89QZFAKBZDZWEUw.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
I/flutter (21905): http://192.168.43.106:3000/cupload\download (1).jfif
I/flutter (21905): http://192.168.43.106:3000/cupload\0157d41e-4db9-4d9a-9632-84c8a4cc15f1.png
D/ViewRootImpl#10bbcbe[MainActivity](21905): ViewPostIme pointer 0
D/ViewRootImpl#10bbcbe[MainActivity](21905): ViewPostIme pointer 1
EDIT.......
i notice a problem, because in browser i got this url for example:
http://192.168.43.106:3000/cupload//1_ir8j2Ag89QZFAKBZDZWEUw.png
but in flutter it is like this:
http://192.168.43.106:3000/cupload%5C1_ir8j2Ag89QZFAKBZDZWEUw.png
there is the problem after (cupload)...
this is the code in node.js
const storage2 = multer.diskStorage({
destination: (req, file, cb)=>{
cb(null, 'cupload');
},
filename: (req, file, cb)=>{
cb(null, file.originalname);
}
});
const filter2 = (req, file, cb)=>{
if(file.mimetype == "image/jpg" || file.mimetype == "image/jpeg" || file.mimetype == "image/png"){
cb(null, true)
}else{
cb(null, false);
}
}
const cupload = multer({
storage: storage2,
filter: filter2
});
app.post('/cat', cupload.single('img'));
app.use('/cupload', express.static('cupload'));
please try using r'snapshot.data[index]['img']' as an Image URL

Exist-db parallel request(bulk request) failed on exist db restxq (post,put)

I am facing a problem on restxq multiple parallel request of post, put. When request from web application as async bulk request (approx. more then 5 ).
I am using exist db 5.1.0 or 5.1.1
I am sending request through angular forkjoin because of i want to submit more then 20 or 50 request in parallel
declare
%rest:path("/docs/{$doc-id}")
%rest:PUT("{$body}")
%rest:consumes("application/json")
%output:method("json")
function doc-rx:update-docs($doc-id as xs:string, $body) {
try{
let $request:= parse-json(util:binary-to-string( $body))
return
<result>Success</result>
}catch * {
<error>Caught error {$err:description}</error>
}
};
Some request will failed with below message, some request got cancelled
Failed Message:
The underlying InputStream has been closed. Unable to encode string value: The underlying InputStream has been closed Some Request got cancelled.
Please advise how can I resolve this issue.

Exist-db Multiple request(post,put) failed on restxq

I am facing a problem on restxq multiple parallel request of post, put. When request from web application as async bulk request (approx. more then 5 ).
declare
%rest:path("/docs/{$doc-id}")
%rest:PUT("{$body}")
%rest:consumes("application/json")
%output:method("json")
function doc-rx:update-docs($doc-id as xs:string, $body) {
try{
let $request:= parse-json(util:binary-to-string( $body))
return
<result>Success</result>
}catch * {
<error>Caught error {$err:description}</error>
}
};
Some request will failed with below message, some request got cancelled
Failed Message:
The underlying InputStream has been closed. Unable to encode string value: The underlying InputStream has been closed Some Request got cancelled.
Please advise how can I resolve this issue.

Google PHP API Offline Access Issue

I am trying to use Google PHP API to sream contents from Google plus. I was able to get the refresh token. But when I tried to get access token using that refresh token, its returning the following error.
Fatal error: Uncaught exception 'apiAuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /var/www/social-media-test/google-api-php-client/src/auth/apiOAuth2.php:242 Stack trace: #0 /var/www/social-media-test/google-api-php-client/src/apiClient.php(281): apiOAuth2->refreshToken('MY-REFRESH-TOKEN-HERE') #1 /var/www/social-media-test/google-api-php-client/examples/plus/p.php(19): apiClient->refreshToken('MY-REFRESH-TOKEN-HERE') #2 {main} thrown in /var/www/social-media-test/google-api-php-client/src/auth/apiOAuth2.php on line 242
MY code is as shown below.
$client = new apiClient();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('client-id');
$client->setClientSecret('secret-key');
$client->setRedirectUri('http://localhost/index.php/main');
$client->setDeveloperKey('dev-key');
$plus = new apiPlusService($client);
$client->refreshToken('MY-REFRESH-TOKEN');
Any Ideas?
you forgot this:
$client->setScopes(array(
'https://www.googleapis.com/auth/blogger'
));
ref: https://developers.google.com/gdata/docs/auth/oauth#Scope

WebRequest time out error

I tried to build an application in Mono using the setting "Mono 2.10.2", but the the following errors.
Unhandled Exception: System.Net.WebException: The request timed out
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00046] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\System\System.Net\HttpWebRequest.cs:813
at System.Net.HttpWebRequest.GetResponse () [0x0000e] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\System\System.Net\HttpWebRequest.cs:825
The bit of code causing this issue is:
string url = "http://webFarm/config.prop";
WebRequest req = WebRequest.Create(url);
WebResponse resp = req.GetResponse();
It failed immediately. But if I switch the setting to "Default Runtime" instead of "Mono 2.10.2", everything is OK. Could anyone help me with this?
Regards,
David
I'm also getting this error using the code:
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("https://api-3t.sandbox.paypal.com/nvp");
objRequest.Timeout =15000;
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
using (StreamWriter myWriter = new
StreamWriter(objRequest.GetRequestStream()))
myWriter.Write(strPost);
causes exception at GetRequestStream() :
System.Net.WebException: The request timed out
at System.Net.HttpWebRequest.GetRequestStream () [0x0005f] in
/usr/src/redhat/BUILD/mono-2.10.2/mcs/class/System/System.Net/HttpWebRequest.cs:752
at NVPAPICaller.HttpCall (System.String NvpRequest) [0x00000] in <filename
unknown>:0
--------------------------------------------------------------------------------
Version information: Mono Runtime Version: 2.10.2 (tarball Mon Apr 18
18:57:39 UTC 2011); ASP.NET Version: 2.0.50727.1433
You should revert back to older Mono 2.10 , 2.10.1 will probably work.

Resources