[英]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 server
来express app
和socket.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.