我正在尝试构建我的聊天应用程序。服务器端和客户端代码已附加。当我安装它时,它成功安装,没有给出任何错误。但它无法与服务器通信,我也希望该服务器可以处理多个客户端。当我将其安装到手机中并成功安装并与服务器通信后,在任何其他设备或仿真器上却无法通信,这是一件令人惊讶的事情。如何使这个具有多个客户端连接的应用程序在服务器上进行聊天?
服务器端脚本
const express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app),
io = require('socket.io').listen(server);
app.get('/', (req, res) => {
res.send('Chat Server is running on port 5000')
});
app.set('port',(process.env.PORT||3000))
io.on('connection', (socket) => {
console.log('user connected. id: '+socket.id)
socket.on('join', function(userNickname) {
console.log(userNickname +" : has joined the chat " );
socket.broadcast.emit('userjoinedthechat ',userNickname +" has joined the chat ");
});
socket.on('messagedetection', (senderNickname,messageContent) => {
//log the message in console
console.log(senderNickname+" :" +messageContent)
//create a message object
let message = {"message":messageContent, "senderNickname":senderNickname}
// send the message to the client side
io.emit('message', message );
});
socket.on('disconnect', function() {
console.log( ' user has left ')
socket.broadcast.emit("userdisconnect"," user has left ")
});
});
server.listen(app.get('port'),function(){
console.log('Node app is running on port ',app.get('port'));
});
**Client side**
尝试{
socket = IO.socket("http://ipaddress:8080").connect();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!messagetxt.getText().toString().isEmpty()){
socket.emit("messagedetection", Nickname, messagetxt.getText().toString());
messagetxt.setText(" ");
}
}
});
socket.on("join", new Emitter.Listener() {
public void call(final Object... args) {
runOnUiThread(new Runnable() {
public void run() {
String data = (String) args[0];
socket.emit("join"+Nickname);
}
});
}
});
socket.on("disconnect", new Emitter.Listener() {
public void call(final Object... args) {
runOnUiThread(new Runnable() {
public void run() {
String data = (String) args[0];
socket.emit("disconnect",Nickname);
// Toast.makeText(ChatBoxActivity.this,data,Toast.LENGTH_SHORT).show();
}
});
}
});
socket.on("message", new Emitter.Listener() {
Message m;
public void call(final Object... args) {
runOnUiThread(new Runnable() {
public void run() {
JSONObject data = (JSONObject) args[0];
try {
//extract data from fired event
String nickname = data.getString("senderNickname");
String message = data.getString("message");
m = new Message(nickname,message);
MessageList.add(m);
chatBoxAdapter = new ChatBoxAdapter(MessageList,ChatBoxActivity.this);
chatBoxAdapter.notifyDataSetChanged();
myRecylerView.setAdapter(chatBoxAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
socket.disconnect();
}
enter code here
答案 0 :(得分:0)
我尝试使用node和html。工作正常
客户端
console.log('Server connection started');
var socket = io();
var date = new Date();
var Nickname = "Karthik"+ date.getTime();
socket.on('join', (data) => {
console.log('join', data);
socket.emit("join", Nickname);
})
socket.on('disconnect', (data) => {
console.log('disconnect', data);
socket.emit("disconnect", Nickname);
})
socket.on('message', (data) => {
console.log('message', data);
// socket.emit("message", Nickname);
})
socket.emit("messagedetection", Nickname, 'Hi Test.');
console.log('Server connected', socket);
请检查发射和开启功能。
服务器端
const express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app),
io = require('socket.io').listen(server);
app.use(express.static('node_modules'))
app.use(express.static('public'))
app.get('/', (req, res) => {
res.send('Chat Server is running on port 5000')
});
app.set('port', (process.env.PORT || 3000))
io.on('connection', (socket) => {
console.log('user connected. id: ' + socket.id)
socket.on('join', function (userNickname) {
console.log(userNickname + " : has joined the chat ");
socket.broadcast.emit('userjoinedthechat ', userNickname + " has joined the chat ");
});
socket.on('messagedetection', (senderNickname, messageContent) => {
console.log(senderNickname + " :" + messageContent)
//create a message object
let message = { "message": messageContent, "senderNickname": senderNickname }
// send the message to the client side
io.emit('message', message);
});
socket.on('disconnect', function () {
console.log(' user has left ')
socket.broadcast.emit("userdisconnect", " user has left ")
});
});
server.listen(app.get('port'), function () {
console.log('Node app is running on port ', app.get('port'));
});