node.js and apache on different servers [closed]

早过忘川 提交于 2019-12-12 06:38:43

问题


I have node.js and socket.io on server A and lamp on server B. Server B is the website that runs everything that my sites need except I want server A to take care of the chat feature I have on server B.

I'm kinda new to node.js and socket.io, But got the hang of sending and receiving messages on server A with a simple index.html.

My question is... what's the best way or how do you send and receive messages from server B to A and back? so I can keep everything I already wrote on server B and just use server A to serve as the chat server for all the messages?

Thanks.


回答1:


Javascript on web server A:

<script src="http://serverB.com/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://serverB.com');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

NodeJS server B:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});


来源:https://stackoverflow.com/questions/20341753/node-js-and-apache-on-different-servers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!