以下是 ii 如何让 codeigniter 和 nodejs 相互交谈。
我的 codeigniter 应用程序在http://mydomain.com/controller/function/上运行,我的 nodejs(nowjs) 在http://mydomain.com:8080上运行,
用户将使用 codeigniter URL,当打开页面时,我的 CI 视图页面上有这个脚本,它连接到我的 Nodejs 应用程序,类似于以下内容:
<script src="http://mydomain.com:8080/nowjs/now.js"></script>
<script>
$(document).ready(function(){
now.receiveMessage = function(name, message){
$("#messages").append("<br>" + name + ": " + message);
}
// Send message to people in the same group
$("#send-button").click(function(){
now.distributeMessage($("#text-input").val());
$("#text-input").val("");
});
now.name = prompt("What's your name?", "");
// on establishing 'now' connection, set server room and allow message sending
now.ready(function(){
// Pick up list of available chatrooms from server and populate dropdown
setServerRoomFromList(now.serverRoomsList);
// By default pick the first chatroom
now.changeRoom($('#server-room').val());
// Connection established and room set; allow user to start sending messages
$("#send-button").removeAttr('disabled');
});
// On change of drop down, clear text and change server room
$('#server-room').change(function(){
$("#messages").html('');
now.changeRoom($('#server-room').val());
});
});
// populate the #server-room dropdown
function setServerRoomFromList(roomList){
$('#server-room').empty();
$.each(roomList, function(key, value)
{
$('#server-room').
append($("<option></option>").
attr("value",key).
text(value));
});
}
</script>
蚂蚁他们可以很好地互相交谈!