首页 > 解决方案 > 在 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

标签: node.jsangularlaravelsocket.iovagrant

解决方案


推荐阅读