使用Google Cloud Messaging云连接服务器(XMPP)的上游文件

时间:2022-09-26 15:15:32

I am implementing Real time Chat using GCM Cloud connection server. I have read the docs Upstream messages Using GCM.I learnt how to upstream message to my server via GCM.It is more reliable as we all know and better than sending messages to webserver via http.

我正在使用GCM云连接服务器实现实时聊天。我已经阅读了文档上游消息使用GCM。我学会了如何通过GCM向我的服务器上传消息。我们都知道并且比通过http向网络服务器发送消息更加可靠。

But now i also want to upload files in chat.but the problem is GCM only allows to upstream maximum of 4KB message.I can use webservices or FTP to upload files to server and then pass the file name to server via GCM.but i want more appropriate solution.So is there any Way to do this via GCM cloud connection server.??

但现在我也想在聊天中上传文件。但问题是GCM只允许上游最大4KB的消息。我可以使用webservices或FTP将文件上传到服务器,然后通过GCM将文件名传递给服务器。但我想要更合适的解决方案。有没有办法通过GCM云连接服务器来做到这一点。

Thanks for the help in advance.

我在这里先向您的帮助表示感谢。

2 个解决方案

#1


0  

The solution that I'm using on this cases is:

我在这个案例中使用的解决方案是:

1 - Use loopj library to send via POST the file to my server (php p.ex)

1 - 使用loopj库通过POST将文件发送到我的服务器(php p.ex)

// loopj supports Http Auth, cookies, params, multipart files, etc. So is a good solution. Like:

...     
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Authorization", "Basic " + Base64.encodeToString("aaa:bbb".getBytes(), Base64.NO_WRAP));

RequestParams params = new RequestParams();
params.put("cmd", "upp");
params.put("uid", Long.toString(getUser().getId()));
params.put("tid", Long.toString(getUser().getIdTeam()));
try{
  params.put("avatar", file); // File object
}catch(FileNotFoundException e){
  ...
}

SharePhotoHandler handler=new SharePhotoHandler(mContext,file,notificate);
client.post(URI_BASE, params, handler);
...

And I use a custom handler to control the file upload (SharePhotoHandler.class) where the app can control

我使用自定义处理程序来控制应用程序可以控制的文件上载(SharePhotoHandler.class)

  • onSuccess
  • onFailure
  • onFinish
  • ...

This methods must let you control the upload/download file state (ok, error, uploading, downloading, etc.)

此方法必须让您控制上传/下载文件状态(确定,错误,上传,下载等)

After the file upload, the server must notificate to the other clients that there are a new file to download or on your "onSuccess" method send a GCM message to the other clients. In my case, the server sends to the other clients the filename (+URL) for download it.

文件上载后,服务器必须向其他客户端通知要下载新文件,或者在“onSuccess”方法上向其他客户端发送GCM消息。在我的例子中,服务器向其他客户端发送文件名(+ URL)以供下载。

#2


0  

you've asked:

So is there any Way to do this via GCM cloud connection server.??

那么通过GCM云连接服务器有没有办法做到这一点。

And the answer for files with greater size than ~4k is - no!

对于大小超过~4k的文件的答案是 - 不!

Explanation: GCM mechanism consists lots of servers and bandwidth that costs Google lots of money. Despite it, Google has provided this world-wide service free of charge in order to encourage developers and users to use Android, by allowing them to get a more efficient experience.

说明:GCM机制包含许多服务器和带宽,这会花费Google很多钱。尽管如此,谷歌还是免费提供这项全球服务,以鼓励开发者和用户使用Android,让他们获得更高效的体验。

Google knew that installing 10+ apps, while each one had to open a connection to a different server in order to get its updates, will slow down the device and will consume expensive bandwidth. So they've implemented the solution of GCM, and that way each device has only one service (Google's) that is constantly searching for updates (instead of many).

谷歌知道安装10多个应用程序,而每个应用程序必须打开到不同服务器的连接才能获得更新,这将减慢设备速度并消耗昂贵的带宽。所以他们已经实现了GCM的解决方案,这样每个设备只有一个服务(谷歌)不断搜索更新(而不是很多)。

Because its a free service, Google had minimized the service to 4k of data. This is good enough because the app will get informed that an update awaits and can then connect to its company's servers to get those updates (instead of constantly searching for them).

由于它是免费服务,谷歌已将服务最小化为4k数据。这是足够好的,因为应用程序将获知更新等待,然后可以连接到其公司的服务器以获取这些更新(而不是不断搜索它们)。

#1


0  

The solution that I'm using on this cases is:

我在这个案例中使用的解决方案是:

1 - Use loopj library to send via POST the file to my server (php p.ex)

1 - 使用loopj库通过POST将文件发送到我的服务器(php p.ex)

// loopj supports Http Auth, cookies, params, multipart files, etc. So is a good solution. Like:

...     
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Authorization", "Basic " + Base64.encodeToString("aaa:bbb".getBytes(), Base64.NO_WRAP));

RequestParams params = new RequestParams();
params.put("cmd", "upp");
params.put("uid", Long.toString(getUser().getId()));
params.put("tid", Long.toString(getUser().getIdTeam()));
try{
  params.put("avatar", file); // File object
}catch(FileNotFoundException e){
  ...
}

SharePhotoHandler handler=new SharePhotoHandler(mContext,file,notificate);
client.post(URI_BASE, params, handler);
...

And I use a custom handler to control the file upload (SharePhotoHandler.class) where the app can control

我使用自定义处理程序来控制应用程序可以控制的文件上载(SharePhotoHandler.class)

  • onSuccess
  • onFailure
  • onFinish
  • ...

This methods must let you control the upload/download file state (ok, error, uploading, downloading, etc.)

此方法必须让您控制上传/下载文件状态(确定,错误,上传,下载等)

After the file upload, the server must notificate to the other clients that there are a new file to download or on your "onSuccess" method send a GCM message to the other clients. In my case, the server sends to the other clients the filename (+URL) for download it.

文件上载后,服务器必须向其他客户端通知要下载新文件,或者在“onSuccess”方法上向其他客户端发送GCM消息。在我的例子中,服务器向其他客户端发送文件名(+ URL)以供下载。

#2


0  

you've asked:

So is there any Way to do this via GCM cloud connection server.??

那么通过GCM云连接服务器有没有办法做到这一点。

And the answer for files with greater size than ~4k is - no!

对于大小超过~4k的文件的答案是 - 不!

Explanation: GCM mechanism consists lots of servers and bandwidth that costs Google lots of money. Despite it, Google has provided this world-wide service free of charge in order to encourage developers and users to use Android, by allowing them to get a more efficient experience.

说明:GCM机制包含许多服务器和带宽,这会花费Google很多钱。尽管如此,谷歌还是免费提供这项全球服务,以鼓励开发者和用户使用Android,让他们获得更高效的体验。

Google knew that installing 10+ apps, while each one had to open a connection to a different server in order to get its updates, will slow down the device and will consume expensive bandwidth. So they've implemented the solution of GCM, and that way each device has only one service (Google's) that is constantly searching for updates (instead of many).

谷歌知道安装10多个应用程序,而每个应用程序必须打开到不同服务器的连接才能获得更新,这将减慢设备速度并消耗昂贵的带宽。所以他们已经实现了GCM的解决方案,这样每个设备只有一个服务(谷歌)不断搜索更新(而不是很多)。

Because its a free service, Google had minimized the service to 4k of data. This is good enough because the app will get informed that an update awaits and can then connect to its company's servers to get those updates (instead of constantly searching for them).

由于它是免费服务,谷歌已将服务最小化为4k数据。这是足够好的,因为应用程序将获知更新等待,然后可以连接到其公司的服务器以获取这些更新(而不是不断搜索它们)。