node.js - 在 Vagrant 上运行节点服务器(socket.io)并从浏览器访问它们
问题描述
我在项目中使用 Laravel 作为后端,出于聊天目的,我使用 Redis。在我的 PC 上,我有 Windows,我在 Vagrant 上运行 Laravel/Redis 应用程序。我还在 Vagrant 上为 socket.io 设置节点服务器,Laravel/Redis 应用程序与该服务器之间的通信工作正常。
问题出在我使用 Angular 的客户端,我无法在 Vagrant 上运行的 socket.io 服务器上连接客户端,我不知道在客户端使用什么地址来订阅。
这是我的节点服务器:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var redis = require('redis');
var client = redis.createClient("redis://127.0.0.1:6379");
var io = require('socket.io')(http);
app.use('/', express.static('www'));
http.listen(3000, '192.168.10.10', function(){
console.log('listening on *:3000');
});
client.on('message', function(chan, msg) {
console.log(chan);
console.log(msg);
io.sockets.emit(chan, msg);
});
client.subscribe('add-message');
在客户端:
const config: SocketIoConfig = { url: 'http://192.168.10.10:3000', options: {} };
但是当我启动应用程序时,我收到应用程序无法订阅服务器的错误。
有谁知道我如何从浏览器或我的专用网络中的其他 IP 地址访问在 Vagrant 上启动的节点服务器?
提前致谢
编辑:这是流浪文件
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'json'
require 'yaml'
VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path(File.dirname(__FILE__))
homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
aliasesPath = confDir + "/aliases"
require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')
Vagrant.require_version '>= 2.1.0'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if File.exist? aliasesPath then
config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases"
config.vm.provision "shell" do |s|
s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases"
end
end
if File.exist? homesteadYamlPath then
settings = YAML::load(File.read(homesteadYamlPath))
elsif File.exist? homesteadJsonPath then
settings = JSON::parse(File.read(homesteadJsonPath))
else
abort "Homestead settings file not found in #{confDir}"
end
Homestead.configure(config, settings)
if File.exist? afterScriptPath then
config.vm.provision "shell", path: afterScriptPath, privileged: false, keep_color: true
end
if Vagrant.has_plugin?('vagrant-hostsupdater')
config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
elsif Vagrant.has_plugin?('vagrant-hostmanager')
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.aliases = settings['sites'].map { |site| site['map'] }
end
end
解决方案
推荐阅读
- excel - 将宏分配给excel中的图像
- android - 工具栏菜单 onOptionsItemSelected 在片段中不起作用
- python - 如何在 Pytest 3.0.7 中并行动态运行一些测试
- windows - 本地组成员身份在重新登录后才更新
- django - Django REST Framework - API 身份验证 - 授权应用程序而不是用户
- amazon-web-services - 通过 API 网关和 Lambda 将文档上传到 S3 存储桶会损坏文档
- node.js - MongoDB - 删除父数组但保留子数组
- mysql - 在一个查询中显示用户评论今天,昨天和其他条件的摘要 优化
- javascript - 在拖放网页中使用 javascript 上传文件
- database - 使用桌面应用程序打包 mongodb 数据库,无需在用户机器上安装 mongodb 服务器