我写了一个"推送服务器",当客户端打开一个连接时,它会在服务器写入流时保持打开状态.我想知道如何编写一个Java客户端,它通过这种无限长的响应来接收命令.
问题是我不知道从哪里开始.如果你能指出我可以阅读Javadoc的课程方向,那就太棒了.我希望它能够立即响应,因此线程可能是必须的.
非常感谢你的帮助!
编辑:无限长度我的意思是未知的长度.无限一词用于强化连接通常永远不会被服务器关闭的点.并没有传输大量数据.
另一个编辑:也许有助于指出这是一个运行PHP脚本的HTTP服务器.
我很好奇是否有办法检测用户使用javascript(或者更好的是,jQuery)按下浏览器中的"停止导航"按钮.例如,如果你点击网页链接需要一段时间来加载,您可能想要显示旋转装载机.但是如果用户取消导航到页面怎么办?反正有没有检测到摆脱你放的旋转装载机?
编辑:我做了一些研究,似乎在javascript中有一个onStop事件,但是,你不知道它,它只适用于Internet Explorer.如果有人有任何其他想法来实现像onStop这样的跨浏览器解决方案,那就太棒了,但如果没有,我会在几天后回答我自己的问题来关闭它.
编辑2:https://stackoverflow.com/a/16216193说这是不可能的.正如其他一些答案一样.
在我的应用程序中,如果连接了网络更改,我需要重新启动服务.目前它只能以一种方式工作(wifi到移动数据),但它不能以其他方式工作(移动数据到wifi.)为什么会这样?是因为我没有进入android.net.wifi.WIFI_STATE_CHANGED我的广播接收器或者可能是错位的许可?
谢谢你的帮助.
代码:接收器的清单条目:
<receiver
android:name="LiveForever"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
接收器本身:
public static final int TYPE_WIFI = 1;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_NOT_CONNECTED = 0;
public static final String PREFS_NAME = "cakecloudpreferences";
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
if (getConnectivityStatus(context)!=0&&!settings.getBoolean("osmframerunning",false)) {
context.stopService(new Intent(context, OSMFrame.class));
settings.edit().putBoolean("osmframerunning",false).commit();
Intent frameintent = new Intent(context,OSMFrame.class);
frameintent.putExtra("username",settings.getString("usr",""));
frameintent.putExtra("password",settings.getString("pswd",""));
frameintent.putExtra("uid",settings.getString("uid",""));
context.startService(frameintent);
Log.i("CCLiveForever","LiveForever Triggered, OSMFrame restarted."); …Run Code Online (Sandbox Code Playgroud) 我发现了一个很好的类来扩展抽象的File Observer类......
import android.os.FileObserver;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
public class FileSync extends FileObserver {
public String absolutePath;
public String uid;
public FileSync(String path, String uidd) {
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
uid = uidd;
}
@Override
public void onEvent(int event, String path) {
if (path == null) { //path is the name of the file... I think its absolute
return;
}
//a new file or subdirectory …Run Code Online (Sandbox Code Playgroud) 我使用此功能编写了一些非常复杂的PHP应用程序,所以我不确定这里发生了什么.
我目前正在编写一个使用在线历史记录来保存内容的应用程序(通过ajax get).我写的用于将用户的历史存储到文件的API非常简单:
$myFile = "./snhistory/".$_GET["uid"];
$stringData = urldecode($_GET["name"])."\n";
$file = fopen($myFile,"a");
fwrite($file,$stringData);
fclose($file);
Run Code Online (Sandbox Code Playgroud)
这看起来像是一种代码,它将添加的数据name加上一行新行添加到文件的末尾,对吧?好吧,这不是PHP看到它的方式.它添加了一个名称,然后当我使用不同的名称运行代码时,它会覆盖第一个,只显示第二个.我尝试过任何我能想到的事情:
file_put_contents($myFile,file_get_contents($myFile).$stringData);
Run Code Online (Sandbox Code Playgroud)
和
file_put_contents($myFile,$stringData,FILE_APPEND);
Run Code Online (Sandbox Code Playgroud)
和
fopen($myFile,"w");
fseek($file,0,SEEK_END);
Run Code Online (Sandbox Code Playgroud)
并且所有都产生相同的行为.这是PHP的错误还是我错过了什么?我觉得如果代码中有问题,我尝试的第二件事就是修复它.但我不确定,这就是为什么我在这里问大家.
我真的很感激可以提供任何帮助.
编辑:根据答案,字符串的问题是它们包含标点符号,'并且由于某种原因,url编码和转义不起作用.阅读答案的编辑以获得最终答案.
我正在制作一个需要键盘的远程应用程序。我没有使用EditText,而是强迫它实用地调用。
在该活动中,我有一个半智能onKeyDown代码,该代码会将android键码转换为服务器可处理的ASCII码,并将其发送:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int asciiKey = -1;
if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) {
asciiKey = keyCode - KeyEvent.KEYCODE_A + 'A';
} else if (keyCode==KeyEvent.KEYCODE_DEL) {
asciiKey = 8;
} else if (keyCode==KeyEvent.KEYCODE_ENTER) {
asciiKey = 13;
} else {
asciiKey = event.getUnicodeChar(event.getMetaState());
}
out.println("k "+asciiKey);
return super.onKeyDown(keyCode, event);
}
Run Code Online (Sandbox Code Playgroud)
但是当我按下Enter键时,它没有发送(我尝试了Jelly Bean Default和Hacker's Keyboard)。甚至没有“ -1”。该方法甚至没有被调用。它适用于大多数其他键(数字,字母,退格键,某些符号),因此它不是应用程序本身。
调用键盘并在以后隐藏它的代码:
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?另外,是否有更好的方法将Android密钥代码转换为Ascii密钥(可通过专门复制java.awt.Robot)。
感谢您的任何帮助。
我希望我错过了一些愚蠢的简单的东西。
我正在尝试从 Ubuntu 15.10 上的源代码编译 ImageMagick。它编译并运行得很好,但缺少对 PNG 的支持。双方libpng并libpng-dev已安装但ImageMagick的是有没有它:
./configure --with-quantum-depth=8 --enable-hdri --with-bzlib=yes --with-jpeg=yes --with-png=yes --with-tiff=yes
...
checking for PNG... no
....
PNG --with-png=yes no
....
Options used to compile and link:
PREFIX = /usr/local
EXEC-PREFIX = /usr/local
VERSION = 6.9.3
CC = gcc
CFLAGS = -fopenmp -g -O2 -Wall -mtune=haswell -fexceptions -pthread -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=8
CPPFLAGS = -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=8
PCFLAGS =
DEFS = -DHAVE_CONFIG_H
LDFLAGS =
LIBS =
CXX = g++
CXXFLAGS = -g -O2 -pthread
FEATURES …Run Code Online (Sandbox Code Playgroud) 我正在编写一个快速脚本来浏览大量音乐,将所有m4as转换为mp3.他们中的大多数已经是mp3,但我希望所有这些都是mp3.这是我到目前为止所拥有的:
for f in *.m4a; do ffmpeg -i "$f" -acodec libmp3lame -ab 320 "${f%.m4a}.mp3"; done
Run Code Online (Sandbox Code Playgroud)
它是在一个层面上,我如何将它集成ffmpeg到find命令中递归执行?
谢谢您的帮助!
android ×3
bash ×1
browser ×1
fileobserver ×1
imagemagick ×1
inputstream ×1
java ×1
javascript ×1
jquery ×1
keyboard ×1
m4a ×1
mp3 ×1
networking ×1
php ×1
server-push ×1
ubuntu ×1