Laravel回声服务器什么也不返回

时间:2019-04-10 12:12:02

标签: php laravel nginx websocket socket.io

我尝试将laravel echo服务器与socket.io一起使用。网络服务器是nginx,我想代理SSL。当我在浏览器中跟踪与开发人员工具的通信时,连接似乎成功,但仅返回“确定”。

这是我的配置:

nginx:

location / {
    try_files $uri /index.php?$query_string;
}

location /socket.io {
    proxy_pass http://localhost:6001/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

# Remove trailing slash to please routing system.
if (!-d $request_filename) {
    rewrite     ^/(.+)/$ /$1 permanent;
}

laravel-echo-server.json

{
   "authHost":"http://localhost",
   "authEndpoint":"/api/v1/broadcasting/auth",
   "clients":[
      {
         "appId":"...",
         "key":"..."
      }
   ],
   "database":"redis",
   "databaseConfig":{
      "redis":{
         "port":"6379",
         "host":"localhost"
      },
      "sqlite":{
         "databasePath":"/database/laravel-echo-server.sqlite"
      }
   },
   "devMode":true,
   "host":null,
   "port":"6001",
   "protocol":"http",
   "socketio":{

   },
   "sslCertPath":"",
   "sslKeyPath":"",
   "sslCertChainPath":"",
   "sslPassphrase":"",
   "apiOriginAllow":{
      "allowCors":false,
      "allowOrigin":"",
      "allowMethods":"",
      "allowHeaders":""
   }
}

js文件

import Vue from "vue";
import Echo from "laravel-echo";
import axios from "axios";
import VueAxios from "vue-axios";
import store from "@/store";
import logout from "@/js/logout";
import { transactionBeep, messageBeep } from "@/js/beep";
import { bus } from "@/bus";

window.io = require("socket.io-client");

export const echo = new Echo({
  broadcaster: "socket.io",
  host: window.location.hostname
});

export const http = axios.create();

Vue.use(VueAxios, http);

http.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (
      error.response &&
      (error.response.status === 401 || error.response.status === 419)
    ) {
      window.location = "/login?authExpired";
    } else {
      if (Vue.notify && error.response.data !== null) {
        if (typeof error.response.data !== "object") {
          Vue.notify({
            group: "errors",
            title: error.response.data,
            type: "error"
          });
        } else if (error.response.data.errors && error.response.data.message) {
          let errorMessages = error.response.data.errors;
          let text = "";
          for (let key in errorMessages) {
            errorMessages[key].forEach(errorText => {
              text += errorText + "<br>";
            });
          }

          Vue.notify({
            group: "errors",
            title: error.response.data.message,
            text: text,
            type: "error"
          });
        } else if (error.response.status) {
          Vue.notify({
            group: "errors",
            title: `Ein Fehler ist aufgetreten. Code: ${error.response.status}`,
            type: "error"
          });
        }
      }
    }

    return Promise.reject(error);
  }
);

export function setCSRFToken(token) {
  // http.defaults.headers.common["X-CSRF-TOKEN"] = token;
  echo.connector.options.auth.headers["X-CSRF-TOKEN"] = token;
}

export function CSRFToken() {
  return document.querySelector('meta[name="csrf-token"][content]').content;
}

setCSRFToken(CSRFToken());

export function joinPrivateChannels() {
  const customerProfileId =
    store.getters["user/me"].customer_profile &&
    store.getters["user/me"].customer_profile[0].id;
  const roles = store.getters["user/me"].roles.map(role => role.name);

  if (roles.includes("customer")) {
    echo
      .private(`message.0.${customerProfileId}`)
      .listen("MessageWasCreated", event => {
        store.commit("message/update", [event]);
        let dialogPartner = event.sender;
        dialogPartner.last_message = event;
        store.commit("dialog/update", [dialogPartner]);
        if (store.getters["user/me"].settings.newMessageSound) {
          messageBeep();
        }
      });

    echo
      .private(`customerProfile.${customerProfileId}`)
      .listen("TransactionWasCreated", transaction => {
        if (transaction.state !== 0) {
          return;
        }

        store.commit("balance/newTransaction", transaction);
        if (
          store.getters["user/me"].settings.flirtDollarSound &&
          transaction.amount > 0
        ) {
          transactionBeep();
        }
      });
  }

  echo
    .private(`user.${store.getters["user/me"].id}`)
    .listen("UserForceLogout", () => {
      logout("/login");
    });

  echo
    .join("online")
    .here(online => {
      store.commit("online/removeLeaver", online.id);
      store.commit("online/update", online);
    })
    .joining(online => {
      store.commit("online/removeLeaver", online.id);
      store.commit("online/update", online);
    })
    .leaving(({ id }) => {
      store.commit("online/addLeaver", id);
      setTimeout(() => {
        if (store.getters["online/leavers"].some(leaver => leaver === id)) {
          store.commit("online/delete", id);
        }
      }, 5000);
    });

  if (roles.includes("moderator")) {
    const modChannel = echo
      .join("moderator")
      .here(moderator => {
        store.commit("moderatorOnline/update", moderator);
      })
      .joining(moderator => {
        store.commit("moderatorOnline/update", moderator);
      })
      .leaving(moderator => {
        store.commit("moderatorOnline/delete", moderator);
      })
      .listenForWhisper("fresh-session", e => {
        if (e.id === store.getters["user/me"].id) {
          logout("/logout-wegen-mehreren-sessions");
        }
      })
      .listen("LogoutFromModeratorChat", data => {
        if (data.id === store.getters["user/me"].id) {
          http.post("/api/v1/moderation/end");
          logout("/logout-wegen-admin");
        }
      });

    setTimeout(() => {
      modChannel.whisper("fresh-session", {
        id: store.getters["user/me"].id
      });
    }, 500);
  }

  if (roles.includes("admin")) {
    echo
      .private("admin")
      .listen("ModeratorMessageOutEvent", data => {
        bus.$emit("waiting-dialogs-update", data);
      })
      .listen("NewAdminTask", adminTask => {
        store.commit("adminBadges/newAdminTask", adminTask);
      });
  }
}

The Service

这是服务,我没有显示日志记录或其他信息。

尤其是当我将nginx配置的一行从proxy_pass http://localhost:6001/;更改为proxy_pass http://localhost:6001/socket.io/时,我得到了类似96:0{"sid":"TZ22xZFXktvjbbDBAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}2:401:1的响应,但通常是{"code":1,"message":"Session ID unknown"} (使用http代码400)。有时我返回了HTTP代码500。通过这种配置,我也没有在服务中显示任何信息。

有解决的主意吗?

0 个答案:

没有答案