简体   繁体   English

在我的 apollo graphQL express 服务器中实现套接字连接

[英]Implement a socket connection in my apollo graphQL express server

I'm running an express based apollo graphQL server using apollo-server-express .我正在使用apollo-server-express运行基于 express 的 apollo graphQL 服务器。

import express from 'express'
import cors from 'cors'
import server from './graphql/schema'

app.use(cors())
server.applyMiddleware({ app, path: '/graphql' })

app.listen(port, async () => {
  if (process.env.NODE_ENV !== 'production') {
    console.log('Listening on port ' + port)
  }
})

export default app

Now I need to connect to some other applications from my client.现在我需要从我的客户端连接到其他一些应用程序。 Therefore he provides me with HL7 data.因此,他为我提供了 HL7 数据。 He told me to 'use a socket to get the HL7 data' , which my application can use.他告诉我“使用套接字获取 HL7 数据” ,我的应用程序可以使用它。 I just don't have a clue how to implement a socket connection at all.我根本不知道如何实现套接字连接。

Doing some researches brought me to libraries like socket.io, which should be used like this (for express):做一些研究让我找到了像 socket.io 这样的库,它应该像这样使用(用于 express):

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000)

I don't understand how to implement the io in my existing code shown above.我不明白如何在上面显示的现有代码中实现io

I never used or implemented a socket connection at all, so I have very big understanding problems with that.我根本没有使用或实现套接字连接,所以我对此有很大的理解问题。 Maybe the socket.io library is not the correct thing for my needs.也许 socket.io 库不适合我的需求。

I do not have any knowlege about HL7 data , I think your another app has been writen by Java.我对HL7 data一无所知,我认为您的另一个应用程序是由 Java 编写的。

But, if you want to implement a socket.io server with apollo-server-express , just follow socket.io official document and attach a http server to express app and socket.io , then start your http server.但是,如果你想用apollo-server-express实现 socket.io 服务器,只需按照socket.io官方文档并附加一个http serverexpress appsocket.io ,然后启动你的 http 服务器。

import express from 'express'
import cors from 'cors'
import GraphQLServer from './graphql/schema'
import socketIO from 'socket.io'
import http from 'http'

let app = express() // You missed this line ?

let httpServer = http.Server()

let io = socketIO(httpServer)

app.use(cors())
GraphQLServer.applyMiddleware({ app, path: '/graphql' })

httpServer.listen(port, async () => { // I don't see your `port`
  if (process.env.NODE_ENV !== 'production') {
    console.log('Listening on port ' + port)
  }
})

io.on('connection', (socket) => {
  console.log('A client connected', socket.id)
});

export default app

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

相关问题 如何在 Apollo 中关闭 GraphQL 订阅的套接字连接 - How to close socket connection for GraphQL subscription in Apollo graphql突变如何在apollo服务器表达式中接受参数数组? - How can a graphql mutation accept an array of arguments with apollo server express? 2022年如何使用apollo server express打开graphql游乐场页面? - How to open graphql playground page using apollo server express in 2022? Apollo 服务器 GraphQL 网络问题 - Apollo server GraphQL network issue 是否可以在快速路由中实现 socket.io 连接? - Is it possible to implement socket.io connection in express route? 如何在GraphQL解析器中访问请求对象(使用Apollo-Server-Express) - How to access the request object inside a GraphQL resolver (using Apollo-Server-Express) 如何使用Apollo在Vue中实现有效的Graphql查询? - How to implement a working Graphql query in Vue with Apollo? Graphql Apollo Server + Vue => 图片上传 - Graphql Apollo Server + Vue => Image Upload 错误:正文必须是字符串。 收到:{ resolvers: [[function HelloResolver]], validate: false } 使用 Type-graphql 和 apollo-server-express 时 - Error: Body must be a string. Received: { resolvers: [[function HelloResolver]], validate: false } when using Type-graphql and apollo-server-express Apollo Server,Graphql-必须提供查询字符串 - Apollo Server, Graphql - Must provide query string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM