简体   繁体   English

使用Node.js在Azure主机上创建Web套接字服务器?

[英]Creating web socket server on Azure hosting using Node.js?

I have a node application I am trying to host on Azure, that create a websocket server. 我有一个节点应用程序,我试图在Azure上托管,创建一个websocket服务器。 I am trying to create the web socket server using the code below: 我正在尝试使用以下代码创建Web套接字服务器:

var socket = require('websocket').server;
...
server.listen(1111,function(){
   console.log('Http server is listening on port 1111');
});

When I do this on my local machine it works fine, but once it's up on Azure the following line of client side javascript: 当我在我的本地计算机上执行此操作时,它可以正常工作,但是一旦它在Azure上运行以下客户端javascript:

var connection = new WebSocket('ws:' + document.domain + ':1111');
connection.send(msg); //throws error

Throws the error: 抛出错误:

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable InvalidStateError:尝试使用不可用或不再可用的对象

Can I get the desired functionality if I'm using azure? 如果我使用azure,我可以获得所需的功能吗? If so, any suggestions on where to start looking for a fix? 如果是这样,有关从哪里开始寻找修复的任何建议?

You can see the broken app in action here . 你可以在这里看到破碎的应用程序。

It will work on Azure, but you need to make a change. 它适用于Azure,但您需要进行更改。 Try this: 尝试这个:

server.listen(process.env.port, function () {
  var addr = app.address();
  console.log('Server listening on http://' + addr.address + ':' + addr.port);
});

Here's a tutorial on using the chat example from Socket.IO's source in Azure: 这是一个使用来自Azure中Socket.IO源代码的聊天示例的教程:

http://www.windowsazure.com/en-us/develop/nodejs/tutorials/app-using-socketio/ http://www.windowsazure.com/en-us/develop/nodejs/tutorials/app-using-socketio/

It assumes you're on Windows, which isn't strictly necessary. 它假定你在Windows上,这不是绝对必要的。 To use Node.js on Azure from a Mac or Linux system, just publish from Git: 要从Mac或Linux系统在Azure上使用Node.js,只需从Git发布:

http://www.windowsazure.com/en-us/develop/nodejs/common-tasks/publishing-with-git/ http://www.windowsazure.com/en-us/develop/nodejs/common-tasks/publishing-with-git/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM