Bonjour les développeurs !
J'ouvre un topic car j'ai un problème que je n'arrive pas a résoudre,
J'ai créer une socket via nodejs qui elle émet un emit qui a pour message "connection ok!" , Ensuite via Android studio je me connecte a ma socket elle émet bien le message "connection ok!" depuis la console mais ce message ne veut pas s'afficher depuis mon TextView.
Dois créer une autre fonction qui a pour but de gérer le TextView? ou alors autre chose? je vous met mon code ci joint:
Mon code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class MainActivity extends AppCompatActivity { TextView MessageSocket; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MessageSocket= (TextView) findViewById(R.id.MessageSocket); runIO(); } public void runIO(){ try { SocketIO socket = new SocketIO("http://10.10.103.36:8080"); socket.connect(new IOCallback() { @Override public void onMessage(JSONObject json, IOAcknowledge ack) { try { System.out.println("Server said:" + json.toString(2)); MessageSocket.setText(json.toString()); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onMessage(String data, IOAcknowledge ack) { System.out.println("Server said: " + data); } @Override public void onError(SocketIOException socketIOException) { System.out.println("an Error occured"); socketIOException.printStackTrace(); } @Override public void onDisconnect() { System.out.println("Connection terminated."); } @Override public void onConnect() { System.out.println("Connection established"); } @Override public void on(String event, IOAcknowledge ack, Object... args) { System.out.println("Server triggered event '" + event + "'"); } }); // This line is cached until the connection is establisched. socket.send("Hello Server!"); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Mon fichier java script
Merci de vos réponses !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 var http = require('http'); var fs = require('fs'); var server = http.createServer(function(req, res) { fs.readFile('./index.html', 'utf-8', function(error, content) { res.writeHead(200, {"Content-Type": "text/html"}); res.end(content); }); }); var io = require('socket.io').listen(server); io.sockets.on('connection', function (socket) { console.log('Connection ok!'); io.sockets.on('connection', function (socket) { socket.emit('message', 'Vous êtes bien connecté !'); }); });
Partager