我目前正在开发一个codeigniter应用程序,其中包含一些nodejs用于实时功能,我集成了nodejs模块,使用socket.io和express开发实时聊天应用程序。我尝试使用php build为我的codeigniter应用程序部署我的应用程序到bluemix,我创建了一个nodejs应用程序来托管我的nodejs服务器。问题是我似乎无法使用io.connect()连接到nodejs服务器套接字。是否有任何方法可以这样做,而无需在代码中进行一些重大更改。 感谢。
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
//var socket = require('socket.io');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// create a new express server
var app = express();
var http = require('http').Server(app);
var io = require('socket.io').listen(http);
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/'));
var people = [];
var left = [];
var connetcted = false;
io.on('connection', function (client) {
console.log("New client !");
console.log("socket.id", client.id);
client.on('message', function (data) {
console.log('Message received ' + data.name + ":" + data.message);
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.emit('message', {name: data.name, message: data.message, to: data.to});
});
client.on('connected', function (data) {
console.log('Message received ' + data);
people[data.current] = data;
connetcted = true;
delete left[data.current];
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.emit('connected', {current: data.current, people: people});
client.on('disconnect', function () {
connetcted = false;
setTimeout(function () {
if (connetcted === false) {
left[data.current] = data;
delete people[data.current];
io.emit('deconnexion', {current: data.current, left: left});
}
}, 10000);
});
});
client.on('notification_client', function (data) {
console.log('Message received ' + data.name + ":" + data.id_client);
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.emit('notification_client', {message: data.message, id_client: data.id_client});
});
client.on('notification', function (data) {
console.log('notification received ' + data.event_id);
//client.broadcast.emit( 'message', { name: data.name, message: data.message } );
io.emit('notification', {message: data.message, event_id: data.event_id, id_tenant: data.id_tenant});
//Pushbots.setMessage(data.message, 1);
//Pushbots.customFields({"event_id": data.event_id});
//Pushbots.customNotificationTitle("CUSTOM TITLE");
//var token = "APA91bGaZBXL9yv-Gr__0TLju-eMP42oKNPJhTtPocf8ar3OLC736tvi26lcXATDnGw7Ode1B2ONcusTvAP7s2L_06jMo8PIQ1QgmRf9k_EBmn-BqWaZLSK026ZQLWUyoIjArgHF6ssP";
//Pushbots.pushOne(token, function (response) {
// console.log(response);
//});
});
});
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
答案 0 :(得分:2)
基于https://github.com/rauchg/chat-example,我能够在Bluemix中使用以下应用。似乎差异可能是调用http.listen()
与app.listen()
:
index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(process.env.PORT, function(){
console.log('listening...');
});
的package.json
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "4.10.2",
"socket.io": "1.2.0"
},
"scripts": {
"start": "node index.js"
}
}
的index.html
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>