我想從插座Android客戶端將數據發送到服務器的Node.js .. 在服務器端做了什麼香港專業教育學院:如何通過回調函數中的Node.js來自Android客戶端
socket.on('new user',function(data,callback){
console.log('ON new user');
if(data in users){
callback(false);
}else {
callback(true);
socket.nickname = data;
users[socket.nickname]= socket;
UpdateNickNames();
}
});
和我的客戶端android:
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
EditText edt;
Button btn;
boolean msg;
private Socket mSocket;
{
try {
mSocket = IO.socket("http://192.168.1.101/");
Log.v(TAG,"fine");
} catch (URISyntaxException e) {
Log.v(TAG,"Error..... "+e.getMessage());
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSocket.connect();
edt=(EditText)findViewById(R.id.editText);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
attemptSend();
}
});
}
private void attemptSend() {
String message = edt.getText().toString().trim();
if (TextUtils.isEmpty(message)) {
return;
}
mSocket.emit("new user", message, true);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
但它的錯誤崩潰: throw er; //未處理'錯誤'事件^ TypeError:回調不是函數
啊哈...ü意思是這樣的: 公共無效回調(布爾X) { 布爾ÿ; y = x; 如果(Y == blablabla){// 代碼放在這裏 }其他{// 代碼放在這裏 } } – AbdoHurbli