var express = require('express')
var https = require('https')
var fs = require("fs")
var path = require('path')
var Constants = require('./includes/Constants.js')
var PORT = Constants.PORT
var webserver = express()
var server = https.Server({
key: fs.readFileSync(path.join(__dirname, 'cert', 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert', 'cert.pem'))
}, webserver)
var io = require('socket.io')(server)
webserver.get("/", (req, res, next) => {
res.sendFile(__dirname+'/index.html');
})
io.on('connection', function(socket) {
console.log("Client connected : " + socket.id)
socket.on(('message'), function(data) {
console.log(data)
})
socket.on('disconnect', function() {
console.log("User disconnected : " + socket.id)
})
})
server.listen(PORT, () => {
console.log('Webserver running on port ' + PORT)
})
PS:如果我运行此代码并访问网站,它会记录“客户端已连接”
安卓代码:
Socket socket;
{
try {
socket = IO.socket("https://<HTTPS ADRESS>:<PORT>");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
socket.connect();
Toast.makeText(this, String.valueOf(socket.connected()), Toast.LENGTH_SHORT).show();
init();
}
Toast 说:“假”并且没有连接到客户端,甚至网络服务器也没有记录新的连接。
请帮忙。