I follow code at page: http://ift.tt/1ylPFRd and download, run successfully project http://ift.tt/1ym2DOL. And I want to connect socket io with my node server
Code at: server nodejs version of socket io "version": "1.3.5",
var socketIO = require('socket.io'),
http = require('http'),
port = process.env.PORT || 8080,
ip = process.env.IP || '192.168.0.105', //My IP address. I try to "127.0.0.1" but it the same => don't run
server = http.createServer().listen(port, ip, function() {
console.log("IP = " , ip);
console.log("start socket successfully");
});
io = socketIO.listen(server);
//io.set('match origin protocol', true);
io.set('origins', ':');
var run = function(socket){
socket.on("message", function(value) {
console.log(value);
});
socket.on("user-join", function(value) {
console.log(value + "user-join");
socket.broadcast.emit("new-users", value);
});
}
io.sockets.on('connection', run);
Code at Android:
package com.example.phamhuu.chatnodejs;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import java.net.URISyntaxException;
public class MainActivity extends ActionBarActivity {
private Socket mSocket;
{
try {
IO.Options options = new IO.Options();
options.port = 8080;
mSocket = IO.socket("http://ift.tt/1G3z7Q1");
//mSocket = IO.socket("http://chat.socket.io");
} catch (URISyntaxException e) {
Log.e("abc", "index=" + e);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSocket.connect();
Log.e("result socket connect", String.valueOf(mSocket.connected()));
mSocket.emit("message", "Send message to server.");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I make sure add 'android.permission.INTERNET' I try it on real device (My PC and my device the same wifi )but socket can't connect to server at address: 192.168.0.105 port: 8080 Can you help me? Thanks very much.
Aucun commentaire:
Enregistrer un commentaire